jQuery ajax XML Http 请求不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1023867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:26:38  来源:igfitidea点击:

jQuery ajax XML Http Request not works

jqueryajax

提问by Andrew Rumm

There is an undefined error due to Ajax request in jQuery. But it works locally. Error referencing in jquery1.3.2.js @ 3633 line

由于 jQuery 中的 Ajax 请求,存在未定义错误。但它在本地工作。在 jquery1.3.2.js @ 3633 行中引用错误

xhr.send(s.data);

My code is:

我的代码是:

$.ajax({
    type: "POST",
    url: 'index.php',
    data: "action=showpath&type=images&path=&default=1",
    cache: false,
    dataType: "html",
    success: function (data) {
        $('#addr').html(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

alerts in code shows me (0, 'undefined');

代码中的警报显示给我 (0, 'undefined');

What I am doing wrong?

我做错了什么?

回答by curthipster

This could be happening if your ajax request is getting canceled before it completes. jQuery throws the error event when the user navigates away from the page either by refreshing, clicking a link, or changing the URL in the browser. You can detect these types of errors by by implementing an error handler for the ajax call, and inspecting the xmlHttpRequest object:

如果您的 ajax 请求在完成之前被取消,则可能会发生这种情况。当用户通过刷新、单击链接或更改浏览器中的 URL 离开页面时,jQuery 会抛出错误事件。您可以通过为 ajax 调用实现错误处理程序并检查 xmlHttpRequest 对象来检测这些类型的错误:

$.ajax({
    /* ajax options omitted */
    error: function (xmlHttpRequest, textStatus, errorThrown) {
         if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) 
              return;  // it's not really an error
         else
              // Do normal error handling
});

回答by Wyatt Barnett

Couldn't tell ya offhand, but it is probably something on the server-side in index.php. Best way to tell is to look at the raw response using a http debugger. The Firebugfirefox extension has a pretty good one, and fidder2is a beefy option.

无法直接告诉你,但它可能是 index.php 中服务器端的东西。最好的判断方法是使用 http 调试器查看原始响应。在Firebug的Firefox扩展有着相当不错的一个,并且fidder2是一个结实的选择。