jquery $.ajax () A small problem solving method for XML data _jquery

Source: Internet
Author: User
The starting code is as follows:
Copy Code code as follows:

$.ajax ({
Type: "Get",
URL: "Database/app_all.xml",
DataType: "xml",
timeout:2000,
Beforesend:function () {},
Success:function (XML) {
$ (XML). Find ("app[id= ' id-1 ')"). Find ("Auther"). Appendto ($ ("#contain"));
},
Error:function () {
Alert ("Ajax failed!");
}
});

That is, the entry with the ID "id-1" is found from the App_all.xml file and will continue to locate the auther tag in its child nodes and add its contents to the ID contain
Div, the XML I'm looking for is <auther>cocept</auther> I just want to get this field out of cocept and put it in a container.

As a result, this code takes effect in Firefox and works in opera, but it doesn't work in Safari and chrome, so I go to the official forum of jquery and Stackflowover
Forum questions, the latter someone replied to me:
I assure you I am using $.ajax with Chrome just five hours ago in the office, and had no such problem.
I also imagine they use it is here in so and I have no problems. I have no problems on jQueryUI in Chrome. I am The It is your code.
The idea is to say that he has no problem with it, is my own problem, I also wondered, and some people back to my suggestions more complex:


You are should use Chrome's or Safari ' s built-in developer tools (Ctrl+shift+i) to track JS errors and track actual AJAX Reque Sts.


Is your code wrapped in Document.ready? Are there any erros in JavaScript console? Also try to output something after success callback line.
Another cause for this could is incorrect mime-type for your the XML file returned by server. It should be [Content-type:text/xml].
You can check this in Chrome ' s or Safari's built-in developer Tools-just look for Headers tab as XML resource is Selec Ted.
If It's actual problem, you'll need to tweak web-server configuration (main config or. htaccess for Apache) to return Co Rrect Mime-type

After all, self-study jquery ajax framework just started ... It's really troublesome to have such a difficult problem.

However, due to a casual discovery, the problem is solved.

I used Firebug to view the status of a successful page element and found that:
Copy Code code as follows:

<div id= "contain" >
<auther>cocept</auther>
</div>

It dawned on me that this method of inserting directly into the pretendto would have even tagname inserted into it, no wonder that Chrome and Safari were not recognizable (in another way Firefox was so much stronger ...). So the revised code is as follows:
Copy Code code as follows:

Success:function (XML) {
$ ("#contain"). HTML ($ (XML). Find ("app[id= ' id-1 ')"). Find ("Auther"). Text ());
}

First remove the value of the text () of the desired element, and then insert the HTML () method HTML () into the container. Tests are passed
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.