-1

I am trying to send json data to an API but it is returning status code 0. I get this error message in the console: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at asm-resumator.azurewebsites.net/resumes. (Reason: CORS header 'Access-Control-Allow-Origin' missing)."

Can you please point out what is wrong or missing with my following code:

var candidate= {name :"Wedad",email:"myemail", phoneNumber:"1234567", resume:"myresume"
};
        $.ajax({
            type: "POST",
            data :JSON.stringify(candidate),
            url: "url",
            dataType: 'json',
        success: function (data) {
                alert(data);
                },
        error: function (jqXHR, exception) {
    var msg = '';
    if (jqXHR.status === 0) {
        msg = 'Not connect.\n Verify Network.';
    } else if (jqXHR.status == 404) {
        msg = 'Requested page not found. [404]';
    } else if (jqXHR.status == 500) {
        msg = 'Internal Server Error [500].';
    } else if (exception === 'parsererror') {
        msg = 'Requested JSON parse failed.';
    } else if (exception === 'timeout') {
        msg = 'Time out error.';
    } else if (exception === 'abort') {
        msg = 'Ajax request aborted.';
    } else {
        msg = 'Uncaught Error.\n' + jqXHR.responseText;
    }
    alert(msg);
}
        });
Wedad Shurrab
  • 95
  • 1
  • 1
  • 7

3 Answers3

2

There is no HTTP status code 0. What you see is a 0 returned by the API/library that you are using. You will have to check the documentation for that. Source

Community
  • 1
  • 1
Vijay Arun
  • 414
  • 8
  • 15
1

As this article explains in details, CORS (cross-origin sharing standard for http access control) requests are made when you requests a resource from a different domain than the page.

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts.

Modern browsers handle the client-side components of cross-origin sharing, including headers and policy enforcement. But this new standard means servers have to handle new request and response headers.

Now, assuming the server has the correct cross origin headers, jQuery ajax has an option to set the correct headers in the request:

If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true.

So, try:

$.ajax({
  type: "POST",
  crossDomain: true, // <= here
  data: candidate, // no need to stringify the data, jquery does it for you
  url: url,
  success: function(data) {
    alert(data);
  }, 
  error: function(error){
    console.log(error);
  }
});

If the error is still there, it probably means the server does not follow the CORS standard, so there is no solution.

Further examples and reading: http://zinoui.com/blog/cross-domain-ajax-request


Just for the sake of knowledge, Chrome and other browser usually do pre-flight requests. In short, they send a first request to the server to know what kind of method is allowed for a cross origin request. cors preflight request

Community
  • 1
  • 1
Derlin
  • 9,572
  • 2
  • 32
  • 53
  • I am sorry. I forgot to mention that I get this error message in the console: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://asm-resumator.azurewebsites.net/resumes. (Reason: CORS header 'Access-Control-Allow-Origin' missing)." – Wedad Shurrab Jan 25 '16 at 04:51
  • asm-resumator.azurewebsites.net/resumes : 404 not found. Could you give me the exact url you use ? – Derlin Jan 25 '16 at 07:46
  • This is the exact url I'm using https://asm-resumator.azurewebsites.net/resumes. I added crossDomain: true, but I still get the same error message "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://asm-resumator.azurewebsites.net/resumes. (Reason: CORS header 'Access-Control-Allow-Origin' missing)." So this means that the server at the other end is not following the CORS standard? – Wedad Shurrab Jan 25 '16 at 16:52
  • Yep, it seems like it. So basically, you can't do anything. What is strange is that i get a 404 not found when following this url... – Derlin Jan 25 '16 at 17:16
  • I'm getting the same. I will get back to person who gave me the url. Thanks! – Wedad Shurrab Jan 25 '16 at 17:22
  • You're welcome. Please mark the question as answered if it's ok. And don't hesitate to come back to stackoverflow if another trouble arises. – Derlin Jan 25 '16 at 17:25
  • Actually, I just knew the data was getting posted to the API and I might be getting this error because I'm submitting the request from a webpage. – Wedad Shurrab Jan 25 '16 at 22:07
0

you can easily send with a html form.

<form enctype="application/json" action="url" method="post">
    <input name="name">
    <input name="email">
    <input name="phoneNumber">
    <input name="resume">
    <input type="submit" name="send" value="Send">
</form>
erman imer
  • 29
  • 4