Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax jquery always running Error

Tags:

Everytime I run my ajax jquery function I get an error, this goes for all my ajax calls. here is an example of my code

 function FindContact(CompanyName,DivisionName,FirstName,LastName) {          $.ajax({             url: 'Path',             dataType: "json",             async:false,             type:'post',             data: {'FirstName':FirstName,'LastName':LastName,'DivisionName':DivisionName,'CompanyName':CompanyName},             success: DisplayContacts,             error: ErrorMsg         });     } 

to get around this I use this

 function ErrorMsg(result) {         if (result.status == 200 && result.statusText == 'OK') {             DisplayContacts(result);         }         else {             alert("FAILED : " + result.status + ' ' + result.statusText);         }     } 

this is tough because I need to create method like this for every ajax request.

why does it run the error code 1st???

Please help!

like image 771
David Avatar asked Apr 21 '11 13:04

David


People also ask

Is AJAX successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards. You should use . done() and use . fail() to catch the errors.

What is jQuery AJAX?

AJAX = Asynchronous JavaScript and XML. In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs. You can learn more about AJAX in our AJAX tutorial.

How does AJAX return an API call?

The A in Ajax stands for asynchronous. That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $. ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.


2 Answers

Ensure that what you are returning is valid json. If its not, and everything goes correct on the server, $.ajax will go to error rather than success.

  function FindContact(CompanyName,DivisionName,FirstName,LastName) {          $.ajax({             url: 'Path',             dataType: "html or json",             async:false,             type:'post',             data: {'FirstName':FirstName,'LastName':LastName,'DivisionName':DivisionName,'CompanyName':CompanyName},             success: DisplayContacts,             error: ErrorMsg         });     }  

A quick to check if the json is valid is to just switch dataType to html and see the success is being fired. If it is than your json is invalid, if your still getting the same problem thers something else wrong.

Another way to check if valid json is being returned is, open up firebug and when the request gets sent, click on the response tab, copy the response and got to jsonlint.com to check if its valid.

Jquery ajax docs

like image 132
locrizak Avatar answered Oct 14 '22 00:10

locrizak


My solution was a difference in Accept-Type and Content-Type headers. If the client-side request's Accept-Type header does not match the server-side response's Content-Type header, Jquery will call the error callback.

Jquery, by default, sets the Accept-Type header to application/json. However, my server was sending a response with Content-Type application/x-javascript. My fix was to change the server-side response Content-Type header to the appropriate application/json.

like image 27
charltoons Avatar answered Oct 13 '22 23:10

charltoons