我有與ASP.NET Web服務看起來像這樣使用jQuery/Javascript來訪問JSON嵌入XML
$(document).ready(function() {
$.support.cors = true;
$.ajax({
type: "GET",
url: "http://www.webservice.com/blahblah.asmx/blahb123",
data: "tnWsGuid=TEST1",
dataType: "text",
success: function(data, status, jqxhr) {
xmlString = data;
alert(xmlString);
},
error: function (request, status, error) {
alert(status);
}
});
});
警報顯示的這個通信的jQuery函數:
<?xml version="1.0" encoding = "utf-8"?>
<string xmlns = "http://Walkthrough/XmlWebServices/">
{"approverName":"","emailAddress":"","companyName":"ABC","address":{"streetAddress1":"12 BlahBlah","streetAddress2":"","state":"ON","zipCode":"","country":"SO","phoneNumber":""},"tabledata:"[{"vendorPart":"AAAAA","partDescription":"N/A","price":"0.00","quantity":"28"},{"vendorPart":"BBBBBBB","partDescription":"N/A","price":"0.00","quantity":"3"},{"vendorPart":"CCCCCC","partDescription":"N/A","price":"0.00","quantity":"25"}]}
</string>
我dataType現在是文本。我知道如何解析JSON,但是現在我需要做的是以某種方式訪問嵌入在XML信封中的JSON並將其轉換爲JSON對象,以便我可以使用JQuery來解析它。
以下是我在$就功能已經嘗試:
success: function(data, status, jqxhr) {
xmlString = data;
var jsondata = jQuery.parseJSON(xmlString.substr(xmlString.indexOf('{')));
alert(jsondata);
}
但隨着無效字符的錯誤,在IE調試look's like this
任何想法如何,我可以訪問返回數據在xml信封裏面並且變成一個JSON對象,所以我可以把它解析爲JSON?我無法更改Web服務,因此必須全部在網頁中完成。
需要注意的是'$ .support.cors = TRUE;除非當前的瀏覽器支持CORS和jQuery不正確地檢測它沒有實際上並沒有做任何事情。 –
是否有你在XML內部擁有JSON的原因,而不是直接返回JSON? –