admin管理员组

文章数量:1404469

 $.ajax({
         url: urlString,
         dataType: "json",
         type: "GET",
         success: function (data) {
             alert(data);
         },
         error: function (jqXHR, exception) {
             if (jqXHR.status === 0) {
                 alert('Not connect.\n Verify Network.');
             } else if (jqXHR.status == 404) {
                 alert('Requested page not found. [404]');
             } else if (jqXHR.status == 500) {
                 alert('Internal Server Error [500].');
             } else if (exception === 'parsererror') {
                 alert('Requested JSON parse failed.');
             } else if (exception === 'timeout') {
                 alert('Time out error.');
             } else if (exception === 'abort') {
                 alert('Ajax request aborted.');
             } else {
                 alert('Uncaught Error.\n' + jqXHR.responseText);
             }

         }
     });

This is my javascript file that im using to access some information from a server. The urlString is supplied and is correct. What I did was download the .json from the server that I was retrieving and accessed it locally on my puter. When I go to access the file from the server I keep getting jqXHR.status==0 error. I'm not sure how to fix that because I can't see anything wrong with my code.

Can someone point me in the right direction to fix my error?

 $.ajax({
         url: urlString,
         dataType: "json",
         type: "GET",
         success: function (data) {
             alert(data);
         },
         error: function (jqXHR, exception) {
             if (jqXHR.status === 0) {
                 alert('Not connect.\n Verify Network.');
             } else if (jqXHR.status == 404) {
                 alert('Requested page not found. [404]');
             } else if (jqXHR.status == 500) {
                 alert('Internal Server Error [500].');
             } else if (exception === 'parsererror') {
                 alert('Requested JSON parse failed.');
             } else if (exception === 'timeout') {
                 alert('Time out error.');
             } else if (exception === 'abort') {
                 alert('Ajax request aborted.');
             } else {
                 alert('Uncaught Error.\n' + jqXHR.responseText);
             }

         }
     });

This is my javascript file that im using to access some information from a server. The urlString is supplied and is correct. What I did was download the .json from the server that I was retrieving and accessed it locally on my puter. When I go to access the file from the server I keep getting jqXHR.status==0 error. I'm not sure how to fix that because I can't see anything wrong with my code.

Can someone point me in the right direction to fix my error?

Share Improve this question asked May 22, 2014 at 9:12 user3664250user3664250 312 gold badges2 silver badges8 bronze badges 2
  • Related, possible duplicate: jquery ajax jqXHR.status is always 0. – Frédéric Hamidi Commented May 22, 2014 at 9:18
  • 1 no I've read that post and it is related but there is no action for the page. It runs when the document is ready. – user3664250 Commented May 22, 2014 at 9:22
Add a ment  | 

3 Answers 3

Reset to default 2

The reason that you get different status codes is that the file isn't fetched with the http: protocol but with the file: protocol. It's natural that different protocols have different status codes.

You simply need to have different behaviour depending on where you fetch the file from.

There is a Mozilla bug report about this, which is marked as invalid because this is considered to be the correct result.

JQXHR Status: 0

Reason: Request does not cancel when the Ajax function is called.

Resolution: Simply add return false; after calling the function, i.e OnClientClick="AJAXMethod(); return false;"

In my case Chrome's 'Network throttling' was set to 'Offline'.

I have removed this error by going to 'Ctrl+Shift+I' > 'Network' tab > click on 'More network conditions...' icon > select 'Network throttling' to 'No throttling'.

本文标签: javascriptAjax jqXHRstatus0 fix errorStack Overflow