The small problem with jquery $. ajax () Getting xml data today

Source: Internet
Author: User

Today, I want to implement an Asynchronous interaction process that reads data from xml using $. ajax ().

The initial 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, find this item with id as "id-1" in the App_all.xml file, continue to find the auther tag in its subnodes, and add its content to the contain
In the div, the content I want to search for in xml is <auther> cocept </auther>. I just want to extract the cocept field and put it in the container.

As a result, this Code takes effect in firefox and opera, but not in Safari and chrome. So I went to the official jQuery forum and StackFlowover.
The Forum asked a question, which was replied to by me:
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.
The general idea is to say that there is no problem with him. It is my own problem. I also wondered, and some people who give me more complicated suggestions later:

You shoshould 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 cocould be incorrect mime-type for your XML file returned by server. It shoshould 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, it's just the beginning of jquery ajax framework ...... This is a tough problem ......

However, due to my accidental discovery, the problem was solved ......

I used firebug to view the page element status after the successful display, and found that:

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

 

 

I suddenly realized that this method of directly Inserting with pretendTo will even Insert the tagname. No wonder chrome and Safari cannot be recognized (in another aspect
Firefox was so powerful ......)
The modified code is as follows:

   success: function (xml) {
$("#contain").html($(xml).find("app[id='id-1']").find("auther").text());
}

 

Take the text () value of the required element and insert it into the container using the html modification method. That's all! All tests 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.