Javascript Jquery—在ajax函数中调用ajax函数
我可以将ajax函数与ajax函数一起使用吗 在我的例子中,有两个ajax调用。第一个ajax将返回一些数据,如果成功,则应调用第二个ajax 下面是我的代码片段Javascript Jquery—在ajax函数中调用ajax函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我可以将ajax函数与ajax函数一起使用吗 在我的例子中,有两个ajax调用。第一个ajax将返回一些数据,如果成功,则应调用第二个ajax 下面是我的代码片段 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", async: false, url: "my service url here" dataType = "json", //success -
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async: false,
url: "my service url here"
dataType = "json",
//success - 1
success: function(data) {
//I ll collect the data from service
//now the second ajax must run.
//Because in first call I ll receive some data
//That data I going to use in my second call
$.ajax({
alert('inside ajax-2');
type: "GET",
contentType: "application/json; charset=utf-8",
async: false,
url: "my second service URL here",
dataType: "json",
//success - 2
success: function(data) {
//some functionality
} //success-2
} //success-1
}); //ajax - 2
}); //ajax - 1
更多信息:
我检查了chrome开发控制台,得到的错误是
//success - 1
success: function(data) {
//Error message : Uncaught SyntaxError: Unexpected identifier
这就是我收到的错误信息
是的,我清除了语法错误,得到了同样的错误信息
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async: false,
url: "my service url here",
dataType : "json"
//success - 1
success: function(data) {
//I ll collect the data from service
//now the second ajax must run.
//Because in first call I ll receive some data
//That data I going to use in my second call
$.ajax({
alert('inside ajax-2');
type: "GET",
contentType: "application/json; charset=utf-8",
async: false,
url: "my second service URL here",
dataType: "json",
//success - 2
success: function(data) {
//some functionality
} //success-2
} //success-1
}); //ajax - 2
}); //ajax - 1
我在firefox浏览器的RESTClient扩展中检查了服务URL,是的,有来自该服务的Jsondata
任何好的建议都是值得赞赏的
圣诞快乐:)您的脚本中有一些错误 在第一个ajax调用中,分隔成员的逗号在哪里
url:"my service url here",
dataType= "json",
这应该是:
dataType : "json",
回到您的答案,是的,您可以,但是,如果您有第三个ajax调用呢?你的代码会很混乱,很难阅读 最好是使用 这是在javascript中使用异步的最佳方式(这也是我评论您的
async:false
)的原因)
你可以读到承诺是如何起作用的
$.ajax
已返回承诺:
var promise = $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url:"my service url here",
dataType: "json",
});
可以与另一个链接:
promise.then(function(result){ });
我倾向于采用将ajax调用拆分为不同的函数的方法,这样可以创建一个新的承诺并返回它;以防我想要操纵结果:
您可以拆分两个ajax调用:
function FirstAjaxCall()
{
var deferred = $.Deferred();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
// async : false,
url:"my service url here",
dataType: "json",
success: function (jsonData) {
deferred.resolve(jsonData);
},
error: function (req, status, error) {
var errorMessage = (error.message) ? error.message : error;
deferred.reject(errorMessage);
}
});
return deferred.promise();
}
及
现在您可以解析第一个并链接第二个:
FirstAjaxCall()
.then(function(result){
return SecondAjaxCall(result);
})
.then(function(result){
// final result
})
.fail(function(reason){
// reason should contain the error.
});
如您所见,FirstAjaxCall()
在.then()
分支中解析,并将其结果传递到匿名函数中。第二个ajax调用SecondAjaxCall()
也会发生同样的情况。如果在第一次或第二次调用中出现故障,错误将被捕获在此处:
.fail(function(reason){
// reason should contain the error.
});
承诺的美妙之处在于,你可以把它们拴在一起或执行它们。是的,你可以
我可以看到您的代码中有一个错误,}//success-1
在}之前//ajax-2
,它应该在之后
另外,在url:“我的服务url在这里”
之后缺少一个coma(,
)
将两种数据类型的“=”替换为“:”
您应该纠正这一点,然后再试一次。以结构化的方式尝试以下内容:
//First method with callback
function myFirstCall(callback) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async:false,
url:"my service url here",
dataType= "json",
success:function(data){
callback();
});
}
// Second method
function mySecondCall() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async:false,
url:"my second service url here",
dataType= "json",
success:function(data){
});
}
//Let's trigger it
myFirstCall(function() {
mySecondCall();
});
您必须将第一个“数据类型”后面的“=”更改为“:”
并将“alert”函数移到第二个$ajax块的外部
$.ajax({ => alert('inside ajax-2');
alert('inside ajax-2'); $.ajax({
最后,结束括号的顺序是相反的
}//success-1 => });//ajax - 2
});//ajax - 2 }//success-1
下面的代码应该按照您的想法工作
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async:false,
url:"my service url here"
dataType : "json",
//success - 1
success:function(data){
//I ll collect the data from service
//now the second ajax must run.
//Because in first call I ll receive some data
//That data I going to use in my second call
alert('inside ajax-2');
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
async:false,
url: "my second service URL here",
dataType: "json",
//success - 2
success: function (data) {
//some functionality
}//success-2
});//ajax - 2
}//success-1
});//ajax - 1
它应该可以工作,但我还没有测试过。Chrome开发控制台中出现了什么错误?是的,我检查了Chrome开发控制台,得到的错误是//success-1 success:function(data){//Error message:Uncaught SyntaxError:Uncaught identifier这是我收到的错误消息您的第一个ajax请求是一个POST。它应该需要一些数据吗?在我看来,这似乎是一个服务器端错误。您的问题是什么?我认为默认情况下,$。ajax返回一个承诺。因此不需要创建延迟承诺return$.ajax(……)是的,我知道。我倾向于使用它,这样我可以在返回结果之前处理结果。也许我应该在我的回答中提到它。干杯。
}//success-1 => });//ajax - 2
});//ajax - 2 }//success-1
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async:false,
url:"my service url here"
dataType : "json",
//success - 1
success:function(data){
//I ll collect the data from service
//now the second ajax must run.
//Because in first call I ll receive some data
//That data I going to use in my second call
alert('inside ajax-2');
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
async:false,
url: "my second service URL here",
dataType: "json",
//success - 2
success: function (data) {
//some functionality
}//success-2
});//ajax - 2
}//success-1
});//ajax - 1