-1

I want to convert a jquery function into a javascript function:

window.parent.$.ajax({
          type: 'GET',
          url: "http://localhost:3063/corsService/GetCultureInformation",
          contentType: "application/json",
          dataType: "json",
          success: function (data) {
              numberDecimalDigit = data.NumberDecimalDigits;
          },
          async: false
      });

I converted it to:

var request = new XMLHttpRequest();
request.open('GET', 'http://localhost:3063/corsService/GetCultureInformation', false);

request.onload = function() {
  if (this.status >= 200 && this.status < 400) {
    // Success!
    numberDecimalDigit = data.NumberDecimalDigits;
    var resp = this.response;
  } else {
    // We reached our target server, but it returned an error

  }
};

request.onerror = function() {
  // There was a connection error of some sort
};

request.send();

but I have errors: 1: XMLHttpRequest: Network Error 0x2efd, Could not complete the operation due to error 00002efd. 2: NetworkError

  • A possible source of answers/duplicate: [Microsoft Edge: XMLHttpRequest: Network Error 0x2efd, Could not complete the operation due to error 00002efd](https://stackoverflow.com/questions/40383565/microsoft-edge-xmlhttprequest-network-error-0x2efd-could-not-complete-the-ope) – Alexander Nied Dec 16 '20 at 06:16

1 Answers1

0

//Better use promises, as it reduces biolerplate heavy code of XMLhttp request provides.

function loadjson(file) {
  return new Promise((resolve, reject) => {
    return fetch(file).then((response) => {
      if (response.ok) {
        resolve(response.json());
      } else {
        reject(new Error("error"));
      }
    });
  });
}

var newFile=loadjson("https://api.postalpincode.in/pincode/110001").then((data) => {
console.log(data);
});