parse xml response with jQuery

前端 未结 4 612
旧巷少年郎
旧巷少年郎 2020-12-01 02:29

HI all,
I use jQuery to parse my xml responses.

I have this xml :


         


        
相关标签:
4条回答
  • 2020-12-01 02:39

    To fix the expected response data type to XML right in your request, set the dataType parameter to "xml". If you don't, jQuery uses the response headers to make a guess.

    It is supported on the $.ajax() function as part of the options object, as well as on $.get() and $.post():

    jQuery.ajax( options )
    jQuery.get( url, data, callback, type )
    jQuery.post( url, data, callback, type )
    

    So you could do this:

    $.ajax({
      type: 'GET',
      url: "foo.aspx",
      data: {
        key: "value"
      },
      dataType: "xml",
      success: function (xml){
        var clientid = $(xml).find('client_id').first().text();
        alert(clientid);
      }   
    });
    

    Note that as of jQuery 1.5 you can use a nicer version of the above Ajax request:

    $.get("foo.aspx", {
      key: "value"
    })
    .done(function (xml){
      var clientid = $(xml).find('client_id').first().text();
      alert(clientid);
    });
    
    0 讨论(0)
  • 2020-12-01 02:47

    Use something like this:

    $.ajax({ type: 'GET', url: 'test.xml', dataType: 'xml', success: function(xml){
                $('response', xml).each(function() {alert($(this).find('client_id').text());});         
                }});
    
    0 讨论(0)
  • 2020-12-01 02:53

    just to complement, i you use $.get:

    $.get($('file.xml').val(),{  } , doSomethingWithData); 
    

    function doSomethingWithData(data) {

     $(data).find("marker").each(function() {
    
    
            var marker = $(this);
    
    
         alert(marker.attr("lat"));
    
     });
    

    here tpicall used request.responseXML is the data in this case, and you have to encapsulate it in $(data) in order to work (this make me break the head about 3 hours ;S)

                         //    alert(markerh);
    
                    }
    
    0 讨论(0)
  • 2020-12-01 02:59

    First, make a request for the XML with $.get or however you want. Then:

    clientID = $(myXML).find("client_id").text();
    
    0 讨论(0)
提交回复
热议问题