Response status is '0' in jquery ajax

  • 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:

        type: "POST",
        url: "",
        data: JSON.stringify({ "loginName": "[email protected]", "mobile": "1234567890" }),
        async: false,
        contentType: "application/json",
        crossDomain: true,
        success: function(data){
        error: function(XMLHttpRequest, textStatus, errorThrown){

    When I execute this, I get this 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?

  • sfdcfox

    sfdcfox Correct answer

    4 years ago

    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.

    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 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.

    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 Your server needs to respond to the OPTIONS request with the appropriate headers. What that means exactly will depend on your server.

    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 In the top section, under "Response Headers", it should say "Access-Control-Request-Methods" (note the S). You're looking at the Request Headers, which are all set okay.

    Ok, you mean, below `Response Headers`, it should show `Access-Control-Request-Method: "POST"`. Am I correct?

    @SSS `Access-Control-Request-Methods` is what the server should respond with, but yes, I think you've got it now.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM