简体   繁体   English

Ajax jqXHR.status == 0修复错误

[英]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.\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. 这是我用来从服务器访问某些信息的javascript文件。 The urlString is supplied and is correct. urlString是提供的并且是正确的。 What I did was download the .json from the server that I was retrieving and accessed it locally on my computer. 我所做的是从我正在检索的服务器下载.json,并在我的计算机上本地访问它。 When I go to access the file from the server I keep getting jqXHR.status==0 error. 当我从服务器访问该文件时,我不断收到jqXHR.status == 0错误。 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? 有人能指出我正确的方向来修复我的错误吗?

The reason that you get different status codes is that the file isn't fetched with the http: protocol but with the file: protocol. 您获得不同状态代码的原因是该文件不是使用http:协议获取的,而是使用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. 有关于此Mozilla错误报告 ,该报告被标记为无效,因为这被认为是正确的结果。

JQXHR Status: 0 JQXHR状态:0

Reason : Request does not cancel when the Ajax function is called. 原因 :调用Ajax函数时请求不会取消。

Resolution : Simply add return false; 解决方案 :只需添加return false; after calling the function, ie OnClientClick="AJAXMethod(); return false;" 调用函数后,即OnClientClick="AJAXMethod(); return false;"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM