views:

636

answers:

3

The ajax call works fine in FF. the data returned is in JSON here is an example from FF firebug -

{"noProfiles": "No profiles have been created, lets start now !"}

When I try to print the error in IE8 (& in compatibility modes as well), it says "parsererror".
But the output seems to be valid JSON.
Here is the ajax function call I'm making.
Any pointers would be great !

$.ajax({   
    type: "GET",   
    url: "/get_all_profile_details/",   
    data: "",   
    dataType: "json",  
    beforeSend: function() {alert("before send called");},  
    success: function(jsonData) {  
        alert("data received");  
    },  
    error: function(xhr, txt, err){  
        alert("xhr: " + xhr + "\n textStatus: " + txt + "\n errorThrown: " + err);  
    }  
 });

The alerts in the error function above give -
xhr:<blank>
textstatus:parsererror
errorThrown: undefined

Any pointers would be great !
Note : jquery : 1.3.2

A: 
  1. Check if the Content-Type header is set to application/json.

  2. Remove dataType: "json", and print the raw response you get in IE8. Maybe a different (not valid JSON) response is sent to IE(8) for some reason

  3. Try with the latest version of jQuery, and see if the problem persists.

Joel L
Currently the content type is something like this --->> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> I inserted application/json in place of text/html and it gave the same error. After that I commented out 'dataType: "json"' but still same error. Trying jquery 1.4.2 now.
PlanetUnknown
Its a django app. Hence the content-type is set as this - return HttpResponse(simplejson.dumps(response_dict), content_type = 'application/json; charset=utf8'). Changed to 1.4.2 but didn't help.
PlanetUnknown
+2  A: 

Here is the solution I finally found !!!

IE is anal about UTF-8, not only that !!
I was formulating my response as below -

return HttpResponse(simplejson.dumps(response_dict), content_type = 'application/json; charset=utf8')

Now FF & Chrome is good with this.
But for IE the utf8 should be like this -
return HttpResponse(simplejson.dumps(response_dict), content_type = 'application/json; charset=UTF-8')

Note the caps UTF -->> UTF-8

To debug the issue, I threw my jquery and wrote a bare bones ajax function.

var xmlhttp = false;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",urlToSend,false);
xmlhttp.send(af_pTempString);
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST",urlToSend,false);
// Do not send null for ActiveX
xmlhttp.send(af_pTempString);
}
//alert("xmlhttp.responseText : " + xmlhttp.responseText);
document.getElementById('navHolder').innerHTML = xmlhttp.responseText;

If the encoding is incorrect, it'll give you this error in IE - c00ce56e

PlanetUnknown
+1  A: 

return HttpResponse(simplejson.dumps(response_dict), content_type = 'application/json; >charset=UTF-8')

Note the caps UTF -->> UTF-8

Works perfectly. I wouldnt even have known about this little intricacies. Atleast give a meaningful error message !!!!

cyrux