Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
934 views
in Technique[技术] by (71.8m points)

javascript - Ajax jqXHR.status==0 fix error

 $.ajax({
         url: urlString,
         dataType: "json",
         type: "GET",
         success: function (data) {
             alert(data);
         },
         error: function (jqXHR, exception) {
             if (jqXHR.status === 0) {
                 alert('Not connect.
 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.
' + 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 computer. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...