Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture 404 status with jQuery AJAX

Tags:

I have this code:

$.ajax({ cache: false,     url: "/Admin/Contents/GetData",     data: { accountID: AccountID },     success: function (data) {         $('#CityID').html(data);     },     error: function (ajaxContext) {         alert(ajaxContext.responseText)     } }); 

I am still confused about the ajaxContext and how to capture 404 return codes. However I have another question. I am reading something about coding with success and fail and no longer using error in the recent versions of jQuery.

So should I change my code to use done and fail. How then could I check for a 404?

like image 664
Alan2 Avatar asked Jun 07 '12 11:06

Alan2


1 Answers

Replace your error function as follows...

error:function (xhr, ajaxOptions, thrownError){     if(xhr.status==404) {         alert(thrownError);     } } 
like image 148
JayTee Avatar answered Oct 02 '22 15:10

JayTee