开发者

How do you find the length of the $(xml) tag from an ajax response

I'm trying to find the length of the xml object that is returned from an ajax request and am 开发者_运维问答trying to determine if the xml in the response is 0 or not. I've tried to use $(xml).length, but it doesn't seem to be working. It return 1 for length regardless of whether their was something returned or not.


I think the issue is the way jQuery does the XML parsing if the content is an empty string or just the XML header, <?xml version="1.0" encoding="UTF-8"?> then it creates an error response with an parseerror node. I created an example here:

http://jsfiddle.net/magicaj/55HQq/1/

If you can control the content on the server then I would suggest always returning at least a response node with the status in it, and then check the child size of the response node:

<response status="success"></response>

In jQuery you can check the size using:

$(xml).find("response").children().size()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜