Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery 1.5.2 displays [object XMLDocument] for empty responses

I have a Url from which I can get a string

If the response string contains something, everything goes well, but (god forbid!) if the result would be an empty string like "" jQuery 1.5.2 will display it as [object XMLDocument]

follow the codes plz :

 $.post('/Applicant/RequestedJob/IsThereActivePeriod',{},
    function(data){     
        if(data == '' ) 
        {
                //do something here!
        }
        else 
        {
            console.log(data.toString());
            // [object XMLDocument]  will be printed in console.
        }        
});

Perhaps I should mention that it used to work perfectly on jQuery 1.4.4 any idea?

Regards :)

like image 268
Javid_p84 Avatar asked Apr 20 '11 07:04

Javid_p84


1 Answers

You should set the expected dataType of the response in your ajax call, like this:

$.post('/Applicant/RequestedJob/IsThereActivePeriod',{},
    function(data){     
        if(data == '' ) 
            openDialog('/Applicant/RequestedJob/AddRequestedJobWindow','pnlRequestedJob','Request Window'); 
        else 
        {
            msgbox.show(data.toString(),'Error', msgBoxButtons.okOnly); 
            console.log(data.toString());
        }
    },
    'html'
);

Without this, jQuery tries to infer the response type, according to this:

Default: Intelligent Guess (xml, json, script, or html).

With no returned content, it's apparently guessing XML. By passing it 'html' as the dataType, you force jQuery to interpret the response as HTML, and store the result in plain text.

As per some of the comments, an appropriate content-type header should allow jQuery to infer that your empty string is HTML, achieving the same result without setting the expected dataType explicitly in the ajax call.

The reason you get [object XMLDocument] is because data is an XML document object, and its toString() is being called.

like image 113
Paul Calcraft Avatar answered Nov 15 '22 10:11

Paul Calcraft