Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript和ajax解析web服务的xml响应_Javascript_Jquery_Ajax_Xml_Web - Fatal编程技术网

使用javascript和ajax解析web服务的xml响应

使用javascript和ajax解析web服务的xml响应,javascript,jquery,ajax,xml,web,Javascript,Jquery,Ajax,Xml,Web,我需要在ajax中解析web服务返回的xml响应,这是我的代码,“响应”是web服务返回的响应,我如何创建xml对象并解析它 $.ajax({ type: 'POST', url: 'web service link', dataType: 'xml:lang', success: function (response) { // how to parse the response here }, error: function (

我需要在ajax中解析web服务返回的xml响应,这是我的代码,“响应”是web服务返回的响应,我如何创建xml对象并解析它

$.ajax({
    type: 'POST',
    url: 'web service link',
    dataType: 'xml:lang',
    success: function (response) {
        // how to parse the response here
    },
    error: function (error) {
        console.log(error);
    }
});
这是我的XML代码:

<ArrayOfMobileConfiguration xmlns:xsd="w3.org/2001/XMLSchema"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns="tempuri.org/">; 
    <MobileConfiguration> 
        <Id>1</Id> 
        <Key>STMaxDownloadSize</Key> 
        <Value>132000</Value> 
    </MobileConfiguration> 
    <MobileConfiguration> 
        <Id>2</Id> 
        <Key>ZoomingThresholdValue</Key> 
        <Value>14</Value> 
    </MobileConfiguration>
</ArrayOfMobileConfiguration>
;
1.
stmax下载大小
132000
2.
缩放阈值
14

jQuery能够以与选择标准HTML相同的方式从XML响应中检索值。试试这个:

$.ajax({
    type: 'POST',
    url: 'web service link',
    dataType: 'xml',
    success: function (response) {
        $('MobileConfiguration', response).each(function() {
            var id = $(this).find('Id').text();
            var key = $(this).find('Key').text();
            var value = $(this).find('Value').text();

            console.log(id, key, value);
        });
    },
    error: function (error) {
        console.log(error);
    }
});
这:

应该是:

dataType: 'xml',

然后,jQuery将忽略响应的内容类型,并将其解析为XML,然后使用它在
success
函数中填充您命名的变量
response

尝试以下代码

$.ajax({
type: 'POST',
url: 'web service link',
dataType: 'xml',
success: function (response) {
   $(response).find('MobileConfiguration').each(function(){
        var Id = $(this).find('Id').text();
        var Key = $(this).find('Key').text();
        var Value = $(this).find('Value').text();

        // Use Id, Key, Value according to your requirement.
   });
},
error: function (error) {
    console.log(error);
}
});

我试图执行这段代码,但如果我在函数display:12和alert(key)display(STMaxDownloadSizeZoomingThresholdValue)中设置了alert(Id),这意味着Id和key的值是相互连接的xml标记的值,我如何循环每个xml标记并对该值进行处理?谢天谢地,这段代码正在这些元素上循环,所以应该可以正常工作。你最好开始一个新的问题,包括你正在使用的代码。好吧,你是对的,我错了,因为我把ArrayOfMobileConfiguration放在了ArrayOfMobileConfiguration而不是MobileConfiguration上
$.ajax({
type: 'POST',
url: 'web service link',
dataType: 'xml',
success: function (response) {
   $(response).find('MobileConfiguration').each(function(){
        var Id = $(this).find('Id').text();
        var Key = $(this).find('Key').text();
        var Value = $(this).find('Value').text();

        // Use Id, Key, Value according to your requirement.
   });
},
error: function (error) {
    console.log(error);
}
});