Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE8 + Jquery ajax call giving parsererror from dJango : for json data which seems valid in Firefox

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

like image 847
PlanetUnknown Avatar asked Apr 17 '10 16:04

PlanetUnknown


1 Answers

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

like image 198
PlanetUnknown Avatar answered Nov 15 '22 01:11

PlanetUnknown