jQuery 同一个ajax调用中的多个url?这可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18097747/
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
Multiple url in same ajax call?is this possible?
提问by HIRA THAKUR
Can I send my data to multiple pages using ajax call? I dont want to use another ajax call for this.
我可以使用 ajax 调用将数据发送到多个页面吗?我不想为此使用另一个 ajax 调用。
Sample code:
示例代码:
$.ajax({
type: 'POST',
url: '../services/form_data.php', //can I send data to multiple url with same ajax call.
data: {
answer_service: answer,
expertise_service: expertise,
email_service: email,
},
success: function (data) {
$(".error_msg").text(data);
}
});
回答by Or Duan
You can't use the same code for 1 request from many pages.
您不能对来自多个页面的 1 个请求使用相同的代码。
BUT you can send 2 requests. its can be done by copy paste your ajax code or by build a function that gets URL and send request with this URL
但是您可以发送 2 个请求。它可以通过复制粘贴您的 ajax 代码或通过构建一个获取 URL 并使用此 URL 发送请求的函数来完成
function sendMyAjax(URL_address){
$.ajax({
type: 'POST',
url: URL_address,
data: {
answer_service: answer,
expertise_service: expertise,
email_service: email,
},
success: function (data) {
$(".error_msg").text(data);
}
});
};
回答by Paul
All you need is $.eachand the two parameter form of $.ajax
var urls = ['/url/one','/url/two', ....];
$.each(urls, function(i,u){
$.ajax(u,
{ type: 'POST',
data: {
answer_service: answer,
expertise_service: expertise,
email_service: email,
},
success: function (data) {
$(".error_msg").text(data);
}
}
);
});
Note: The messages generated by the success callback will overwrite each other as shown. You'll probably want to use $('#someDiv').append() or similar in the success function.
注意:成功回调生成的消息将相互覆盖,如图所示。您可能希望在成功函数中使用 $('#someDiv').append() 或类似方法。
回答by RichieHindle
A single AJAX request can only talk to one URL, but you can create multiple AJAX requests for different URLs, and they will do their work simultaneously - you wouldn't need to wait for one to complete before firing off the next one.
单个 AJAX 请求只能与一个 URL 通信,但您可以为不同的 URL 创建多个 AJAX 请求,它们将同时完成工作——您无需等待一个请求完成后再触发下一个请求。
(If your URLs are on the same server, you could consider refactoring your server-side code so that a single URL does all the work that your multiple URLs do now.)
(如果您的 URL 位于同一台服务器上,您可以考虑重构您的服务器端代码,以便单个 URL 完成您的多个 URL 现在所做的所有工作。)
回答by Joaquin Lopez
Using '$.when' could be an alternative as well. I have used it in one of my project.
使用 '$.when' 也可以是一种替代方法。我在我的一个项目中使用过它。
Reading jQuery API instructions:
阅读 jQuery API 说明:
Execute the function myFunc when both ajax requests are successful, or myFailure if either one has an error.
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ) .then( myFunc, myFailure );
当两个 ajax 请求都成功时执行函数 myFunc,如果其中一个请求有错误则执行 myFailure。
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ) .then( myFunc, myFailure );