简体   繁体   中英

Jquery - parse XML received from URL

I have this URL, that I supposedly should receive an XML from. So far I have this:

    function GetLocationList(searchString)
    {

  $.ajax({
    url: "http://konkurrence.rejseplanen.dk/bin/rest.exe/location?input=" + searchString,
    type: "GET",
    dataType: "html",
    success: function(data) {

    //Use received data here.
    alert("test");

    }
});

Tried to debug with firebug, but it doesn't go into the success method. Though, in DreamWeaver it is able to post a simple alert, which is inside the success method. I tried writing xml as dataType, but it doesn't work (in DreamWeaver) when I write alert(data). But it shows an alert with the entire XML, when I write html as dataType.

How do I get the XML correctly, and how do I parse and for example get the "StopLocation" element?

Try to add an Error function as well.

See enter link description here

This will give you all the informations you need to debug your code with Firefox.

$.ajax({
    url: "http://konkurrence.rejseplanen.dk/bin/rest.exe/location?input=" + searchString,
    type: "GET",
    dataType: "html",
    success: function(data) {

    //Use received data here.
    alert("test");

    },
    error: function(jqXHR, textStatus, errorThrown ){
      // debug here
    }
});

you need to parse it first, and then you can search for the attributes. like this.

success: function(data) {
        var xml = $.parseXML(data)
        $(xml).find('StopLocation').each(function()
    {
        var name = $(this).attr('name');
        alert(name);
    }       
    );

this will give you the name of each StopLocation.

hope this helps, you can use the same method for all other attributes in the document also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM