筆記:Jquery拿取Xml回傳值要注意的地方
先建立一個簡單的泛型處理常式(Handle.ashx)
context.Response.ContentType = "text/xml";
context.Response.Write(str);
context.Response.End();接下來建立一個Default的網頁,這裡我們分別要來測試1.2.6版與1.4.2版 Jquery API的差異
首先引入JS(1.2.6版)
<script type="text/javascript" language="javascript">
google.load("jquery", "1.2");
</script>撰寫Javascript
url: "Handle.ashx",
contentType: "text/xml",
type: 'POST', //使用POST
data: xmlString,
dataType: 'xml',
processData: false,
error: function (xhr, textStatus, thrownError) {
alert("Error:" + textStatus);
},
success: function (xml) {
}
});以上程式執行,並不會發生任何的錯誤,程式也都很正常的運行
接下來我們引入JS(1.4.2版)
<script type="text/javascript" language="javascript">
google.load("jquery", "1.4");
</script>執行後,會得到一個ParseError的錯誤訊息,如果type是用POST的話,切記要在1.4.2版需要在加入這一段才能正常WORK
url: "Handle.ashx",
contentType: "text/xml",
type: 'POST', //使用POST
async:false, //要多加這一個 By 1.4.2 Jquery
data: xmlString,
dataType: 'xml',
processData: false,
error: function (xhr, textStatus, thrownError) {
alert("Error:" + textStatus);
},
success: function (xml) {
}
});參考: