Home  >  Article  >  Web Front-end  >  jquery $.ajax() small problem solution to get xml data_jquery

jquery $.ajax() small problem solution to get xml data_jquery

WBOY
WBOYOriginal
2016-05-16 18:15:451113browse

The starting code is as follows:

Copy code The code is 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, from App_all. Find the item with the id "id-1" in the xml file, and continue to find the auther tag in its child node, and add its content to the div with the id contain
. I want to find it in the xml The content is cocept. I just want to take out the cocept field and put it into the container

As a result, this code takes effect in firefox and opera, but in It didn't work in Safari and Chrome, so I went to the official jQuery forum and asked a question in the StackFlowover
forum. Someone from the latter replied:
I assure you I was using $.ajax with Chrome just five hours ago at the office, and had no such problem.
I also imagine they use it here on SO and I have no problems here. I have no problems on jQueryUI in Chrome. I think it is your code. He said that there is no problem in using it, but it is my own problem. I was also puzzled. Someone later gave me more complicated suggestions:


You should use chrome's or safari's built-in developer tools (ctrl shift i) to track JS errors and track actual AJAX requests.


Is your code wrapped in document.ready? Is there any erros in javascript console? Also try to output something after success callback line.
Another cause for this could be incorrect mime-type for your XML file returned by server. It should be [Content-type: text/xml].
You can check that in chrome's or safari's built-in developer tools - just look for headers tab when xml resource is selected.
If it 's actual problem, you may need to tweak web-server configuration (main config or .htaccess for apache) to return correct mime-type

After all, I just started to learn the jquery ajax framework by myself... It is really troublesome to encounter such a difficult problem...

However, due to an inadvertent discovery, the problem was solved...

I used Firebug checked the page element status after success and found:

Copy code The code is as follows:
< ;div id="contain">
cocept


I suddenly realized that this method of inserting directly using pretendTo Even the tagname is inserted, no wonder Chrome and Safari cannot recognize it (from another aspect, Firefox is so powerful...) So the modified code is as follows:

Copy the code The code is as follows:
success: function (xml) {
$("#contain").html($(xml).find( "app[id='id-1']").find("auther").text());
}

First take out the value of text() of the required element , and then insert it into the container using the html() method to modify html, and you're done! All tests passed
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:flexigrid parameter description_javascript skillsNext article:flexigrid parameter description_javascript skills

Related articles

See more