0

I'm trying to add a new element using Post method, using the following function. But i'm getting error :

readyState=0, status =0 ,statusText=error.

So what could be the error, is it from the url, that I'm trying to send (I changed in the snippet)?

$('document').ready(function() {
  sendData();
});

function sendData() {
      console.log("start");
      var arr = { api_token: 'fb24085da58dad6decb9271fb170ef2ed8c80617',restaurantId : 500 ,name: 'cookies' ,img:'images'};
    
      $.ajax({
          url: 'http://URLChanged?api_token=fb24085da58dad6decb9271fb170ef2ed8c80617',
          type: 'POST',
          data: JSON.stringify(arr),
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
          async: true,
    
          success: function(response){
              console.log("success");  
              console.log(JSON.stringify(response));
          },
          error: function(err){
              console.log("error");  
              console.log(JSON.stringify(err));
          }
      });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I'm getting in console this error :

XMLHttpRequest cannot load http://ChangedURL/restaurants/3/menus?api_token=fb24085da58dad6decb9271fb170ef2ed8c80617. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. testAjax.html:115 {"readyState":0,"status":0,"statusText":"error"}

Amina
  • 723
  • 8
  • 20
  • 2
    I'm going to guess that the URL you are posting to is not the same domain as the page you're posting from. – lonesomeday Dec 01 '16 at 15:38
  • 1
    The error handler function has more than just 1 argument returned. Add the missing ones and using a `debugger` statement in your error handler you can inspect their values in the debugger console. Those should show you more details hopefully. See [jQuery.ajax docs](http://api.jquery.com/jquery.ajax/) for more details on the error handler. – Nope Dec 01 '16 at 15:40
  • lonesomeday, what do you mean by different domain ? thank you – Amina Dec 01 '16 at 15:50
  • after running the new code i'm getting this error in console : XMLHttpRequest cannot load http://changedURL/menus?api_token=fb24085da58dad6decb9271fb170ef2ed8c80617. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. testAjax.html:115 {"readyState":0,"status":0,"statusText":"error"} – Amina Dec 01 '16 at 15:55
  • 1
    "Different domain" — Your HTML document is loaded from a file:// URI on your hard disk and has no domain (i.e. `null`), the URL you are POSTing to is `http://URLChanged` so has the `URLChanged` domain. `null` and `URLChanged` do not match. – Quentin Dec 01 '16 at 15:59
  • Thank you. Isthere any method to fix this issue ? – Amina Dec 01 '16 at 16:01

0 Answers0