jQuery:在成功函数中获取 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9830707/
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
jQuery: get url in success function
提问by StackOverflowed
I would like to get the url from a jQuery success function, after it retreives data from the server. Unfortunately, the three parameters the success function takes don't expose the original url:
在从服务器检索数据后,我想从 jQuery 成功函数中获取 url。不幸的是,success 函数采用的三个参数没有暴露原始 url:
success: function(data, statusText, jqhxr)
success: function(data, statusText, jqhxr)
I dumped the jqhxr variable, and couldn't find the url in there. The reason I need the url is because I'm making several calls at once through a loop, and therefore I have no idea which Ajax call is coming back. Making it an asynchronous call didn't help either.
我转储了 jqhxr 变量,但在那里找不到 url。我需要 url 的原因是因为我通过一个循环同时进行了多次调用,因此我不知道哪个 Ajax 调用会返回。使其成为异步调用也无济于事。
Thank you in advance!
先感谢您!
回答by Esailija
this.url
inside success function will work because this
refers to the current context of the function, and since the success function is part of the settings object that you're passing to .ajax()
it will access the url property.
this.url
内部成功函数将起作用,因为它this
指的是函数的当前上下文,并且成功函数是您传递给.ajax()
它的设置对象的一部分,它将访问 url 属性。
回答by Eric Hodonsky
you mean making it a synchronous call didn't help...?
你的意思是让它成为一个同步调用没有帮助......?
and my suggestion would be to set a parameter in the data you're getting back to determine what to do with it, if you're looping through a bunch of AJAX calls. But really you should have to loop through more than one call if you're doing it right you should be able to send enough parameters along with the data to get it all back in one call, this reduces the number trips between client and server, server and database, then server back to client.
我的建议是在您返回的数据中设置一个参数以确定如何处理它,如果您正在循环执行一堆 AJAX 调用。但实际上,如果您做得对,您应该循环多次调用,您应该能够发送足够的参数和数据,以便在一次调用中将其全部恢复,这减少了客户端和服务器之间的次数,服务器和数据库,然后服务器返回到客户端。
回答by amit_g
You could probably use context when making the ajax call.
在进行 ajax 调用时,您可能会使用上下文。
$.ajax({
url: "test1.html",
context: $("#call1")
});
$.ajax({
url: "test2.html",
context: $("#call2")
});
<div id="call1" class="hidden" data-url="whatever1"></div>
<div id="call2" class="hidden" data-url="whatever2"></div>
then in
然后在
success: function(data, statusText, jqhxr) {
var url = $(this).data("url");
}
That way you could get any data, not just the url. Put any data you want to access in the corresponding div element.
这样你就可以得到任何数据,而不仅仅是 url。将您要访问的任何数据放在相应的 div 元素中。
回答by Stefan
If you call the server script via jQuery .ajax() or .post() or .get(), then you must have the url available in the success callback function, even if you are using a loop. That is because you have to pass the url to all three of those methods, and the result returned is specific to the url that you pass.
如果您通过 jQuery .ajax() 或 .post() 或 .get() 调用服务器脚本,那么您必须在成功回调函数中具有可用的 url,即使您使用的是循环。那是因为您必须将 url 传递给所有这三个方法,并且返回的结果特定于您传递的 url。
For example:
例如:
for(var i=0; i<10; i++) {
url = "action_remote_" + i + ".php";
$.ajax({
type:'POST',
url: url,
data:$('#' + str_form_id).serialize(),
success: function(response) {
alert(url);
}
});
}
Unless I am not understanding you correctly...
除非我没有正确理解你......