使用Jquery的Ajax函数处理来自ASP.NET Web服务的响应

时间:2013-04-15 11:58:53

标签: javascript jquery ajax json web-services

我的回复来自ASP.NET网络服务

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

记录的标题信息的链接

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我已经解决了这个问题,并成功让它在IE上返回数据集(由于两者的异步问题尚未在Chrome和Firefox上工作,我希望在托管此页面时会修复它。我添加了{{1在ajax函数之前,我的新成功函数正确地显示了这样的数据:

$.support.cors = true;