0

我有来自 ASP.NET Web 服务的响应

<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>

使用此 jquery 函数调用它:

$(document).ready(function() {
                $.ajax({
                type: "POST",
                url: "http://www.webservice.com/blahblah.asmx/blahb123",
                data: "tnWsGuid=TEST1",
                dataType: "script",
                    success: function(msg)
                    {
                        alert("sucess")
                    },
                    error: function(e)
                    {
                        alert(JSON.stringify(e));                       
                    }
                    });
            });

我的第一个问题是,我有 403 个禁止使用此功能的问题,但省略 contentType 改变了这一点。然后我遇到了 XML 解析问题,一时兴起将 dataType 更改为脚本修复了该问题并给了我一个响应,然后我点击了成功功能。为什么这行得通?

我也想知道如何打印出这些数据,因为试图将其视为 json 是行不通的,XML 也行不通。

在 chrome 中,我收到此警告:

Resource interpreted as Script but transferred with MIME type text/xml: "http://www.webservice.com/blahblah.asmx/blahb123?tnWsGuid=TEST1&_=1366025879568."

附加到此网址末尾的部分让我感到困惑(在 TEST1 之后)。在控制台中,我也在 chrome 中收到此错误:

Uncaught SyntaxError: Unexpected token < 

Firebug 给了我:

SyntaxError: syntax error
 [Break On This Error]  

<?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 是脚本,我得到了某种响应,但仍然不知道如何解析这些数据。最好变成一个html表格。

我希望你能帮忙!谢谢阅读!

编辑:

这是 Firebug在此处记录的标头信息的链接

4

2 回答 2

1

当服务器发送一个 json 数组时,您需要将数据类型设置为 json。请参阅此处的数据类型部分http://api.jquery.com/jQuery.ajax/

于 2013-04-15T15:58:29.807 回答
0

我已经解决了这个问题,并成功地让它在 IE 上返回数据集(由于两者的异步问题,我希望在托管此页面时能够解决这个问题,因此尚未在 Chrome 和 Firefox 上工作。我$.support.cors = true;在 ajax 函数和我的新成功函数正确显示了如下数据:

function(data, status, jqxhr) { xmlString = data;
alert(xmlString); }

于 2013-04-17T13:47:37.943 回答