简体   繁体   English

如何使用promise获取失败的ajax请求的URI?

[英]How to get the URI of a failed ajax request using promises?

I have this simple code: 我有这个简单的代码:

var jqXHR = $.getJSON(uri);
jqXHR.done(function (ajax_data) { /* put here your code */ });
jqXHR.fail(function (jqXHR, lvl, msg) {
    console.error(lvl + " with AJAX call: " + msg);
});

Is there a way to get uri from the jqXHR object in the fail() handler? 有没有办法从fail()处理程序中的jqXHR对象获取uri There's no jqXHR.url attribute, in opposition of the jqXHR parameter of the error() handler of $.ajax() . 没有jqXHR.url属性,与$.ajax()error()处理函数的jqXHR参数相反。

I could use jqXHR.responseText , but it's the whole response from the server and it's a bit too much for me. 我可以使用jqXHR.responseText ,但这是服务器的全部响应,对我来说有点太多了。

PS: I'm using jQuery promises for ease of use. PS:我正在使用jQuery promises以便于使用。 If I've time I'll use the Q library . 如果我有时间,我将使用Q库

There's no jqXHR.url 没有jqXHR.url

Try this.url 试试this.url

 var jqXHR = $.getJSON("../"); // `uri` : `"../"` jqXHR.done(function (ajax_data) { /* put here your code */ }); jqXHR.fail(function (jqXHR, lvl, msg) { console.error(lvl + " with AJAX call: " + msg, this.url); // `error with AJAX call: ../` }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> 

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

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