admin 管理员组文章数量: 1087649
I have code like that
var ajaxrequest = $.ajax({
type: "POST",
dataType: "json",
url: "xy.php",
data: {
action : "read"
}
}).fail(function(){
//something to do when ajaxreq fails
}).done(function(data){
//something to do when ajaxreq is done
});
It is working no problem. My question is why this doesnt work:
var ajaxrequest = $.ajax({
type: "POST",
dataType: "json",
url: "n3_vaje_api.php", //Relative or absolute path to response.php file
data: {
action : "read",
},
fail:function(){
//something to do when ajaxreq fails
},
done:function(data){
//something to do when ajaxreq is done
}
});
Fail and done are just examples, plete also doesnt work if used inside. But using it outside like:
ajaxrequestplete(f(){});
is working just fine... I know instead of done I should use success, but thats not my point here. Whats the deal here?
I have code like that
var ajaxrequest = $.ajax({
type: "POST",
dataType: "json",
url: "xy.php",
data: {
action : "read"
}
}).fail(function(){
//something to do when ajaxreq fails
}).done(function(data){
//something to do when ajaxreq is done
});
It is working no problem. My question is why this doesnt work:
var ajaxrequest = $.ajax({
type: "POST",
dataType: "json",
url: "n3_vaje_api.php", //Relative or absolute path to response.php file
data: {
action : "read",
},
fail:function(){
//something to do when ajaxreq fails
},
done:function(data){
//something to do when ajaxreq is done
}
});
Fail and done are just examples, plete also doesnt work if used inside. But using it outside like:
ajaxrequest.plete(f(){});
is working just fine... I know instead of done I should use success, but thats not my point here. Whats the deal here?
Share Improve this question asked Jan 20, 2015 at 11:38 zmajericzmajeric 531 gold badge1 silver badge6 bronze badges 2-
1
you need to use
success
anderror
– Arun P Johny Commented Jan 20, 2015 at 11:45 - dont forget to upvote and accept answer as it worked for you.. – Pranay Rana Commented Jan 20, 2015 at 14:40
3 Answers
Reset to default 7you need to use success and error is the method you need to use if you want to use your second option
this is example of ajax request without promise, where you are getting success and error function as parameter
$.ajax({url:"demo_test.txt"
,error : function (xhr,status,error)
{ //alert error}
,success:function(result){
$("#div1").html(result);
}});
In the first opetion you are using promise object return by ajax requst that is the reason you are getting done and fail method.
this is example of promise object , in below example request is promise object
var request = $.ajax({
url: "script.php",
type: "POST",
data: { id : menuId },
dataType: "html"
});
request.done(function( msg ) {
$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.plete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
You could simply use $.post instead of $.ajax and json as the fourth argument.
$.post("n3_vaje_api.php", {action : "read"}, function(response) {
// Do something with the request
}, 'json')
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
本文标签: javascriptjQuery AJAX request eventsdone fail successStack Overflow
版权声明:本文标题:javascript - jQuery AJAX request events - done,fail,success - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1741415057a2294743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论