Skip to main content
June 28, 2016
Question

CORS request not working

  • June 28, 2016
  • 2 replies
  • 3206 views

I'm currently trying to retrieve and send data from Marketo's API. The problem is : my web platform is Salesforce Community. If I understand correctly this web tool, I'm not allowed to use anything else than pure javascript.

I've built a CORS request like this :

function createCORSRequest(method, url) {

  var xhr = new XMLHttpRequest();

  if ("withCredentials" in xhr) {

    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    xhr = new XDomainRequest();

    xhr.open(method, url);

  } else {

    xhr = null;

  }

  return xhr;

}

function makeCorsRequest() {

  var url = document.getElementById('url').value;

  var xhr = createCORSRequest('GET', url);

  if (!xhr) {

    alert('CORS not supported');

    return;

  }

  xhr.onload = function() {

    var text = xhr.responseText;

    alert('Response from CORS request to ' + url + 'is : ' + text);

  };

  xhr.onerror = function() {

    alert('Woops, there was an error making the request.');

  };

 

  xhr.send();

}

With the help of http://www.html5rocks.com/en/tutorials/cors/, but the server doesn't seem to accept the request since this error comes back :

"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'testurl…' is therefore not allowed access."

Does anyone know if Marketo's API accepts CORS requests? Or maybe have an idea that would help me solve this? Thank you very much.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Kenny_Elkington
Adobe Employee
Adobe Employee
June 28, 2016

I responded over on StackOverflow: Call Marketo API using javascript only - Stack Overflow​  There are probably other options depending on what exactly you're trying to accomplish.

SanfordWhiteman
Level 10
June 28, 2016

The REST API should never be used by directly customer-facing applications.  It's intended to be used in server-to-server contexts, preferably using bulk operations.  If you explain in full what you are actually trying to accomplish, as Kenny said, we can give you some pointers.