5

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 :)

7
  • is openDialog jquery ui dialog??
    – S L
    Apr 20, 2011 at 7:48
  • yes! but the point is somewhere else!!! ;-) it is value of the data parameter
    – Javid_p84
    Apr 20, 2011 at 7:51
  • you mean you get [object XMLDocument] in dialog box??
    – S L
    Apr 20, 2011 at 7:52
  • no! value of data which was supposed to be an empty string is [object XMLDocument]
    – Javid_p84
    Apr 20, 2011 at 7:54
  • means somewhere you have some header header('content-type: xml/doc')
    – S L
    Apr 20, 2011 at 7:56

1 Answer 1

9

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.

1
  • Excellent explanation Paul! I actually thought I would never find the answer to this one because in my case it was happening only in Firefox browser (other browsers didn't render it even though I didn't specify the dataType). Gotta love StackOverflow!!!
    – Marko
    Feb 27, 2012 at 16:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.