jQuery AJAX request events - done,fail,success
10,277
you 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 );
});
Related videos on Youtube
Author by
zmajeric
Updated on June 14, 2022Comments
-
zmajeric about 2 years
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, complete also doesnt work if used inside. But using it outside like:
ajaxrequest.complete(f(){});
is working just fine... I know instead of done I should use success, but thats not my point here. Whats the deal here?
-
Arun P Johny over 9 yearsyou need to use
success
anderror
-
-
zmajeric over 9 yearsIts a school assignment and I used first thing I got working. Also AJAX request was REQUIRED so that was my option.
-
zmajeric over 9 yearsThank you.I kinda understand now
-
Pranay Rana over 9 years@user3271259 - you can read about promise object on jquery site or google ...and dont forget to upvote and accept answer