我有一个 jquery 函数,它与一个看起来像这样的 ASP.NET Web 服务通信
$(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);
}
});
});
警报显示是这样的:
<?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>
我的数据类型现在是文本。但是我知道如何解析 JSON,现在我需要做的是以某种方式访问嵌入在 XML 信封中的 JSON 并将其转换为 JSON 对象,以便我可以使用 JQuery 来解析它。
这是我在 $.ajax 函数中尝试过的:
success: function(data, status, jqxhr) {
xmlString = data;
var jsondata = jQuery.parseJSON(xmlString.substr(xmlString.indexOf('{')));
alert(jsondata);
}
但是返回了一个无效字符的错误,在 IE 调试器中看起来像这样
知道如何访问 xml 信封内的数据并将其转换为 JSON 对象,以便将其解析为 JSON 吗?我没有能力更改网络服务,所以这一切都必须在网页内完成。