Alex Rivera | Logout

ajax jquery always running Error

Asked 2011-04-21T13:26:59.663
27

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!

Edit
Report

1 Answer

3

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.

answered 2013-06-12T16:07:25.757

Your Answer