我的問題是我想從服務器返回一個xml文件返回給客戶端,並使用jQuery的ajax函數解析它。這是代碼:在jQuery中返回xml 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>";
現在的作品完美。感謝您的快速回復!
是否$(XML).find( 「醫生」)工作嗎? – 2011-05-18 23:20:02