开发者

jQuery 1.5.2 displays [object XMLDocument] for empty responses

开发者 https://www.devze.com 2023-02-27 11:44 出处:网络
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

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){     
开发者_C百科        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 :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消