0

I am running the below javascript code in Ubuntu from Kaiosrt(Firefox OS) and getting readystate=4 ,but i am not getting the status as 200 and also the response is null.I am not getting any CORS error in Ubuntu.

I tired the same in windows as well, in Firefox WebIDE and came across the same issue i.e status=0 and response=null with CORS error in windows.

I am unable to solve this issue,it would be really helpful if some can suggest any solution for this .

<html>
<body>
    <button type="button" onclick="onbtnSubmit()">LoginD</button>
    <div id="demo"></div>
</body>

<script>

function onbtnSubmit() { 

var data ={"id":"12345670","pass":"123456"};

var xhr = new XMLHttpRequest();

xhr.withCredentials = true;

xhr.open("POST", "https://apiurl");

xhr.send(data);
   xhr.addEventListener("readystatechange", function() {
   console.log("readyState="+this.readyState);

  if(this.readyState === 4) {

    console.log("status="+this.status);
    console.log("response="+this.responseText);
    document.getElementById("demo").innerHTML = this.status+" "+this.responseText;

     }
    }); 
    return false;
}

</script>
</html> 
Shruti
  • 81
  • 5
  • Just go to networks tab and check the request and response objects, you will get to know the error. – Prabhjot Singh Kainth Mar 17 '20 at 06:22
  • i checked in the network tab and it correctly shows the data send in params tab ,however the response tab is empty – Shruti Mar 17 '20 at 07:05
  • That means your API is not sending response to the request, kindly check code of your API URL. – Prabhjot Singh Kainth Mar 17 '20 at 07:31
  • @PrabhjotSinghKainth, I had checked that by running the server on localhost and there i was able to see my request and also the response send by the api ,but i didn't got that response on my side(client) as the text was still empty . Also had checked the api code by calling the same api in android and it works fine there i.e provides response in android . – Shruti Mar 17 '20 at 08:11
  • Then it must be issue of CORS, check on google how to Allow Access Control Origin. – Prabhjot Singh Kainth Mar 17 '20 at 09:42

0 Answers0