我的问题是我想将一个 xml 文件从服务器返回给客户端并使用 jquery 的 ajax 函数解析它。这是代码:
客户:
$("#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);
}
});
});
服务器(php文件),
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";
我有一个空白警报,我不知道为什么?
好的,我找到了。我的 php 文件是这种形式
//some code
include("other.php");
//some other code
other.php 文件是我在上面发布的文件。我剪切/粘贴标题,所以最终的 php 文件将是
//some code
header('Content-type: text/xml');
include("other.php");
//some other code
和other.php
echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";
现在它完美运行了。感谢您的快速回复!
最佳答案
一切正常
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);
}
});
}
关于jquery - 在 jquery ajax 中返回 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6052092/