Skip to content Skip to sidebar Skip to footer

Javascript Function Onload Never Called

I wish to get a json file with a get request from a webservice, and I have a 'cross origin request', so I check 'withCredentials', and then I use the onload function to display the

Solution 1:

You need to

xhr.send();

functioncreateCORSRequest(method, url) {
  var xhr = newXMLHttpRequest();

  if ("withCredentials"in xhr) {
    xhr.open(method, url, true);
    console.log("withCredentials is true");

    xhr.onload = function() {
      console.log('ok');
      if (xhr.status >= 200 && xhr.status < 400) {
        data.forEach(card => {
          console.log(card.nameEnglish1);
        })
      } else {
        console.log('error');
      }
    };

    xhr.onerror = function() {
      console.log('There was an error!');
    };

    // added send call here:
    xhr.send();

  } elseif (typeof XDomainRequest != "undefined") {
    xhr = newXDomainRequest();
    xhr.open(method, url);
  } else {
    xhr = null;
  }
  return xhr;
}

var xhr = createCORSRequest('GET', 'http://google.com/');
if (!xhr) {
  thrownewError('CORS not supported');
}

Post a Comment for "Javascript Function Onload Never Called"