在 jquery ajax 中返回 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6052092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:18:40  来源:igfitidea点击:

return xml in jquery ajax

jqueryxmlajaxtypesreturn

提问by Christos Mitsis

My problem is that I want to return an xml file from server back to client and parsing it using ajax function of jquery. This is the code:

我的问题是我想将一个 xml 文件从服务器返回给客户端并使用 jquery 的 ajax 函数解析它。这是代码:

Client:

客户:

$("#submit").click(function(){          
    $.ajax({  
        type: "POST",  
        url: "search.php",  
        data: "whatever",
        dataType: "xml",
        async: false,
        success: function(xml){
            var data = $('doctor',xml).text();
            alert(data);
        }
    });
});

Server(php file),

服务器(php文件),

header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";

I have a blank alert and I do not know why??

我有一个空白警报,我不知道为什么?



好的,我找到了。我的 php 文件是这种形式

//some code
include("other.php");
//some other code

where the other.php file was the file I posted above. I cut/paste the header so the final php file would be

other.php 文件是我在上面发布的文件。我剪切/粘贴标题,因此最终的 php 文件将是

//some code
header('Content-type: text/xml');
include("other.php");
//some other code

and other.php

和其他.php

echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";

now it works perfect. Thanks for your quick replies!

现在它完美无缺。感谢您的快速回复!

回答by Satyam

This is working fine

这工作正常

Post.php file

Post.php 文件

if($_GET['id']!=""){    
    $array = array('satyam'  => 'satyam',
                   'class'   => 'B.TECH',
                   'company' => 'Ranosys');
}   

$new ='<?xml version="1.0" encoding="iso-8859-1"?><data>';
foreach($array as $key => $values){ 
    $new .= "<$key>$values</$key>";
}
echo $new.'</data>';

=================

function load_data(){
    $.ajax({
        url: "post.php",
        async: false, // stop browser for another activity
        data: "id=satyam",
        // dataType :'xml',
        error: function(e, b, error) { 
            for(var i in e){
              // alert(i);
            }
            alert(e.respone);
        },
        success: function(msg) {
            //alert($response);
            var data = $(msg).find("satyam").text();
            alert(data);
        }
    });
}

回答by Kevin Ennis

Try this: var data = $(xml).find('doctor').text()

尝试这个: var data = $(xml).find('doctor').text()

In your example, 'xml' is not a jQuery object.

在您的示例中, 'xml' 不是 jQuery 对象。

回答by David Rodrigues

You need to parse this XML (I really don't understand why , but...), you can do it by do:

你需要解析这个 XML(我真的不明白为什么,但是......),你可以这样做:

$(xml).find('doctor').text();

Bye. :)

再见。:)

回答by Avitus

You have to change your function to be:

您必须将您的功能更改为:

$("#submit").click(function(){       
    $.ajax({  
        type: "POST",  
        url: "search.php",  
        data: "whatever",
        dataType: "xml",
        async: false,
        success: function(xml){

            var xmlDoc;

            if (window.DOMParser) {
                parser = new DOMParser();
                xmlDoc = parser.parseFromString(xml, "text/xml");
            }
            else {// Internet Explorer
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = "false";
                xmlDoc.loadXML(xml);
            }
            var $response = $(xmlDoc);
            var data = $response.find("doctor").text()

            alert(data);
        }
    });
});

The reason for the if (window.DOMParser) {is that you'll have an issue with IE doing the parsing.

if (window.DOMParser) {的原因是您在 IE 进行解析时会遇到问题。