javascript 一个ajax调用中的2个网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27872840/
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
2 urls in one ajax call?
提问by Larcy
I was wondering if I can do this jquery ajax on my code:
我想知道我是否可以在我的代码上执行这个 jquery ajax:
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_add.php", "functions/ajaxNca_update.php",
data: dataString,
cache: false,
success: function(response){
// show success
alert(response.a);
}
The code above is just an example and I knew that it's not working. How can I call 2 php script in just one ajax request in jquery ? Can someone help ?
上面的代码只是一个例子,我知道它不起作用。如何在 jquery 的一个 ajax 请求中调用 2 个 php 脚本?有人可以帮忙吗?
回答by Salvador Dali
No you can not do this. If you will look at jquery ajax documentation, you will see that url accepts only string, not an array of stings.
不,你不能这样做。如果您查看jquery ajax 文档,您会看到 url 只接受字符串,而不接受字符串数组。
You should either make two requests, or create another *.php
entry point which will combine both php scripts and make a call to this one.
您应该发出两个请求,或者创建另一个*.php
入口点,该入口点将结合两个 php 脚本并调用此脚本。
回答by ABIRAMAN
You can't call 2 url simultaneously.but you can call one after another.
您不能同时调用 2 个 url,但可以一个接一个调用。
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_add.php",
data: dataString,
cache: false,
success: function(response){
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_update.php",
data: dataString,
cache: false,
success: function(response){
//responce
}
});
}
});
or
或者
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_add.php",
data: dataString,
cache: false,
async: true,
success: function(response){
//responce
}});
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_update.php",
data: dataString,
cache: false,
async: true,
success: function(response){
//responce
}});
async: true - synchronizes your ajax calls
async: true - 同步您的 ajax 调用
回答by Niko Jojo
More simple way :
更简单的方法:
You can also do one ajax call to functions/ajaxNca_add.php
file, and include second file
functions/ajaxNca_update.php
in first ajax file.
您还可以对functions/ajaxNca_add.php
文件进行一次 ajax 调用,并将第二个文件包含
functions/ajaxNca_update.php
在第一个 ajax 文件中。
So from one ajax call, you can access 2 files at a time.
因此,从一个 ajax 调用中,您可以一次访问 2 个文件。
回答by Shams Reza
You can define the object first then you need to call ajax two times by changing the url.
您可以先定义对象,然后您需要通过更改 url 调用 ajax 两次。
var ajaxObj = {
type: "POST",
data: thisForm.serialize(),
success: function (response) {
if(response.status=="success"){
dataLayer.push({'event' : 'requestdemo_form_submitted', 'request_demo_submit_event_gtm' : 'request_demo_submit_event_gtm'});
$('.sidebar-req-sec').addClass('hide');
$('.sidebar-req-sec-thnk').removeClass('hide');
$('.sidebar-req-sec-thnk').addClass('show');
}
}
};
//url1 ajax
ajaxObj.url = 'url1';
jQuery.ajax(ajaxObj);
//url2 ajax
ajaxObj.url = 'url2';
jQuery.ajax(ajaxObj);