1

I'm using a jquery ajax for storing a data in the database. I call a service through a url and send data to it in the following way:

$.ajax({
    type: "POST",
    url: "http://192.168.250.118:8080/rest_service/rest/user",
    data: JSON.stringify({ "loginName": "tsample@gmail.com", "mobile": "1234567890" }),
    async: false,
    contentType: "application/json",
    crossDomain: true,
    success: function(data){
        alert("success");
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    }
});

When I execute this, I get this error:

{"readyState":0,"status":0,"statusText":"error"}

This is what I get when I look for errors in network:

enter image description here

But, if I try using postman, the data is getting stored and I get 200OK response. What's wrong with the above code? What should I change?

2

A Status Code of 0 means "the browser refused to honor the request." Generally, this might happen because of a Content Security Policy, a pre-flight check failure, or because the site is not in the same network as the Internet (most browsers differentiate between local and public internet connections, and restrict public internet from reaching private networks). There's nothing wrong with your code, but the browser is simply refusing to honor the request. Make sure that your server is sending the appropriate Access-Control-* headers with each request. Postman isn't having an issue only because, when installed, requests permission to access any site, and so isn't normally affected by Content Security Policy settings or Access-Control-* headers.

8
  • Thanks for your response. I don't understand "appropriate Access-Control-* headers". What does that mean? I've shown the headers which is being shown by the network. Isn't it appropriate?
    – SSS
    Feb 1 '17 at 8:44
  • @SSS Your Reponse Headers do not seem to include Access-Control-Request-Methods or Access-Control-Allow-Origin, which should include "POST' and "*", respectively, for those headers.
    – sfdcfox
    Feb 1 '17 at 9:24
  • Oh! Ok. I get it. What is the solution for that, bro? Should I change something in the code? Or what should I do??
    – SSS
    Feb 1 '17 at 9:27
  • @SSS Your server needs to respond to the OPTIONS request with the appropriate headers. What that means exactly will depend on your server.
    – sfdcfox
    Feb 1 '17 at 9:29
  • BTW, Access-Control-Request-Method: "POST" is there na? In the above pic, below the request header? You said, it is not included, but it shows it?
    – SSS
    Feb 1 '17 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.