Javascript 在回调函数中访问 jQuery Ajax 请求的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5468312/
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
Access the URL of an jQuery Ajax Request in the Callback Function
提问by Chris W.
Is there a way that I can see the URL that was requested when I do an Ajax request with jQuery?
当我使用 jQuery 执行 Ajax 请求时,有没有办法可以看到请求的 URL?
e.g.,
例如,
var some_data_object = { ...all sorts of junk... }
$.get('/someurl.php',some_data_object, function(data, textStatus, jqXHR) {
var real_url = ? # <-- How do I get this
})
How can I access the URL that jQuery actually used to make the request? Perhaps some method/property of jqHXR
? I couldn't find it in the documentation.
如何访问 jQuery 实际用于发出请求的 URL?也许某些方法/属性jqHXR
?我在文档中找不到它。
Thanks.
谢谢。
回答by airbai
Set a break point in success method, then watch
在成功方法中设置断点,然后观察
this.url
is the real url for the request.
是请求的真实网址。
回答by Dror
Here is a possible solution:
这是一个可能的解决方案:
- Catch the ajax call beforeit is sent to the server by implementing the beforeSend callback function.
- Save the url and the data
Report it in the error message you generate.
var url = ""; $.ajax({ url: "/Product/AddInventoryCount", data: { productId: productId, trxDate: trxDate, description: description, reference: reference, qtyInCount: qtyInCount }, //encodeURIComponent(productName) type: 'POST', cache: false, beforeSend: function (jqXHR, settings) { url = settings.url + "?" + settings.data; }, success: function (r) { //Whatever }, error: function (jqXHR, textStatus, errorThrown) { handleError(jqXHR, textStatus, errorThrown, url); } }); function handleError(jqXHR, textStatus, errorThrown, url) { //Whatever }
- 通过实现 beforeSend 回调函数,在发送到服务器之前捕获 ajax 调用。
- 保存网址和数据
在您生成的错误消息中报告它。
var url = ""; $.ajax({ url: "/Product/AddInventoryCount", data: { productId: productId, trxDate: trxDate, description: description, reference: reference, qtyInCount: qtyInCount }, //encodeURIComponent(productName) type: 'POST', cache: false, beforeSend: function (jqXHR, settings) { url = settings.url + "?" + settings.data; }, success: function (r) { //Whatever }, error: function (jqXHR, textStatus, errorThrown) { handleError(jqXHR, textStatus, errorThrown, url); } }); function handleError(jqXHR, textStatus, errorThrown, url) { //Whatever }
回答by Chris Bloom
Using $.ajaxPrefilter
:
使用$.ajaxPrefilter
:
// Make sure we can access the original request URL from any jqXHR objects
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
jqXHR.originalRequestOptions = originalOptions;
});
$.get(
'http://www.asdf.asdf'
).fail(function(jqXHR){
console.log(jqXHR.originalRequestOptions);
// -> Object {url: "http://www.asdf.asdf", type: "get", dataType: undefined, data: undefined, success: undefined}
});
回答by jotaelesalinas
FYI, as an addition to airbai's comment -I cannot comment inside his answer,- you can add your own data to the call and retrieve it inside the callbacks. This way you don't have to parse the URL.
仅供参考,作为airbai 评论的补充-我无法在他的回答中发表评论,-您可以将自己的数据添加到通话中并在回调中检索它。这样您就不必解析 URL。
In this example JSONP request I have added the variable user_id
(tested with jQuery 3.2):
在这个示例 JSONP 请求中,我添加了变量user_id
(使用 jQuery 3.2 测试):
var request = $.ajax({
dataType: "json",
url: "http://example.com/user/" + id + "/tasks?callback=?",
user_id: id,
success: function(data) {
console.log('Success!');
console.log("User ID: " + this.user_id);
},
timeout: 2000
}).fail(function() {
console.log('Fail!');
console.log("User ID: " + this.user_id);
});
回答by Paul I
It seems like the ajaxSend global handler (http://api.jquery.com/ajaxSend/) provides the url in its settings parameter. You could store a mapping from the xhr object to the url in your ajaxSend callback, then in your success callback look it up given the xhr object that it provides you with.
似乎 ajaxSend 全局处理程序(http://api.jquery.com/ajaxSend/)在其设置参数中提供了 url。您可以在 ajaxSend 回调中存储从 xhr 对象到 url 的映射,然后在您的成功回调中根据它提供的 xhr 对象查找它。
var mappings = {};
$.ajaxSend(function(event, xhr, settings) {
mappings[xhr] = settings.url;
});
$.ajax({
url: "http://test.com",
success: function(data, textStatus, xhr) {
console.log("url = ", mappings[xhr]);
delete mappings[xhr];
}
});
This has the advantage of not having to modify each $.ajax() object.
这具有不必修改每个 $.ajax() 对象的优点。
回答by JeremyWeir
I couldn't find it in the docs either. Maybe just add it to the jqXHR object through a "proxy" wrapper like...
我也无法在文档中找到它。也许只需通过“代理”包装器将其添加到 jqXHR 对象,例如...
I haven't tested this, so you may need to call
$.param()
and concat to the url. See http://api.jquery.com/jQuery.param/
我还没有测试过这个,所以你可能需要调用
$.param()
并连接到 url。见http://api.jquery.com/jQuery.param/
var myGet = function(url, data, success) {
$.get(url, data, function(data, textStatus, jqXHR) {
jqXHR.origUrl = url; // may need to concat $.param(data) here
success(data, textStatus, jqXHR);
});
}
usage:
用法:
var some_data_object = { ...all sorts of junk... }
myGet('/someurl.php',some_data_object, function(data, textStatus, jqXHR) {
var real_url = jqXHR.origUrl;
})