开发者

Firefox processing response from servlet

I have a javascript which receives info from a servlet using jQuery:

$.get("authenticate", {badge:$('input#badge').val()}, function(data) {
        console.log("xml: "+data);   
        displayInfoReturn(data);
    });

When I process the result in Safari, everything works great:

    function displayInfoReturn(data) {
        if (/load/.test(data)) { // ...process string 
        }
    }

But the 'if' always returns false in firefox (haven't tried it yet in IE or Chrome). I开发者_如何学JAVA also tried using indexOf != -1 and search != -1. Nothing works!

One curious thing I noticed however is when I print data to console:

console.log ("received... "+data);

it comes back with "received... [object XMLDocument]". So apparently it's not treating my data as a string. I tried data.toString() but that doesn't work either. So how can I get firefox to play fair here?


What does your servlet return? An application/xml document or just text/plain? What have you set in response.setContentType()? You seem to be expecting XML and Firefox seems to be telling that it's really an XML document, but yet you're treating it as text/plain with that regex .test(). I'm not sure about Safari, but it look like that it has overridden a toString() on the XML document object so that it returns the whole XML string so that your regex by coincidence works fine.

Without knowing the exact XML document it's hard to tell how exactly to fix it. If it's for example

<data>
    <action>load</action>
</data>

Then you can check the presence of load value in the <action> tag using jQuery's own XML parsing facilities as follows:

function displayInfoReturn(data) {
    if ($(data).find('action').text() == 'load') {
        // ...
    }
}

See also:

  • Easy XML consumption with jQuery
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜