Below, am making an AJAX call. First it has to hit the dofirstthing.do. On success of it, it has to make a call with "param1" as the query parameter.
My question - I need to make another call with "param2" as the query parameter after above call. Not sure how to?
$.ajax({
url: "dofirstthing.do",
jsonp: false,
type: "GET",
dataType: "JSON",
statusCode: {
200: function () {
global.location.search = param1;
}
}
});
You can chain the ajax calls like this
$.ajax({..}).then(function(data1){
return $.ajax({..});
}).then(function(data2){
return $.ajax({..});
})
You can execute a function on success or fail as this:
var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
});
reference: http://api.jquery.com/jquery.ajax/
Try using jQuery deferred.then()
, defining an error function
var err = function err(jqxhr, textStatus, errorThrown) {
console.log(errorThrown)
}
$.ajax({
url: "dofirstthing.do",
jsonp: false,
type: "GET",
dataType: "JSON",
statusCode: {
200: function() {
global.location.search = param1;
$.get("/url?" + "param1=" + global.location.search)
.then(function() {
$.get("/url?" + "param2=" + param2).then(null, err)
}, err)
}
}
});
jQuery offers a $.get()
function that returns a promise.
You can chain your requests.
see https://api.jquery.com/deferred.then/ for more info
You can do it with a chain of callbacks ---
function sendAjax(target,callback)
{
$.ajax({
url: target,
jsonp: false,
type: "GET",
dataType: "JSON",
statusCode: {
200: callback
}
});
}
Now call Your function as -
sendAjax("dofirstthing.do",function(data){
sendAjax("dofirstthing.do?param1="+data,function(data){
sendAjax("dofirstthing.do?param2="+data,function(data){
return ;
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.