Search code examples
javascriptapixmlhttprequesthttpresponseweb-frontend

net::ERR_EMPTY_RESPONSE for XMLHttpRequest POST Javascript


This might be a duplicate question. I tried other solution but did not meet with the result. I have the following function sending some data to API, but getting

net::ERR_EMPTY_RESPONSE

error. If I try this API URL with exactly the same data in POSTMAN, there I get 200 response with the output. But in a javascript file, it's failing with readystate 1. Please help me with the solution.

function sendSignTransaction(address,balance,nonce,type,input1,privatekey){
  var xhttp = new XMLHttpRequest();
  xhttp.onload = function() {
    if (this.readyState == 4 && this.status == 200) {
      var data = JSON.parse(this.response);
      console.log(this.responseText);
    }
  };
  xhttp.open("POST", "http://3.112.106.186:9997/signTransaction",true);
  xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  var trinput = JSON.stringify({
    "fee": 100000000000000000,
    "address": address,
    "balance": balance,
    "nonce": nonce,
    "type": type,
    "input": input1,
    "PrivateKey": privatekey,
    "crypto": "cic"
  });
  xhttp.send(trinput);
}

Solution

  • I referred this link, https://salesforce.stackexchange.com/questions/158448/response-status-is-0-in-jquery-ajax and got to know that there is a problem in API settings, allowing cross-origin and all. When API developer does changes, my problem solved.