无法从jQuery中使用ASP.NET Web Service

Jam*_*mes 4 asp.net jquery json web-services

这是一个有趣的问题:

我有一些看起来像这样的jQuery:

    $(document).ready(function() {
    $.ajax({
       type: "POST",
       url: "http://localhost:63056/Service1.asmx/PrintOrderRecieptXml",
       data: {"CouponCode":"TESTCODE","Subtotal":14.2600,"ShippingTotal":7.5000,"TaxTotal":0.0000,"GrandTotal":21.7600,"OrderItemCollection":[{"Total":14.2600,"Qty":250}]},
       dataType: "json",
       contentType: "application/json",
       error: function(xhr, msg) { alert(xhr.statusText); }
    });});
Run Code Online (Sandbox Code Playgroud)

现在,我遇到的问题是它正在发送请求,但是Web服务没有正确处理它.在IE中,我收到一个带有"内部服务器错误"的警告框,而在FireFox中我收到一个没有任何内容的警告框.

奇怪的是,当我使用IE时,我的事件日志中没有出现错误事件,但是使用firefox我得到了(为了弄清楚为什么会这样做的奖励积分):

"异常消息:对于意外以'/ PrintOrderRecieptXml结尾的URL,无法识别请求格式"

我戳了一下,发现有时你必须添加:

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost" />
    <add name="HttpPostLocalhost"/>

  </protocols>
</webServices>
Run Code Online (Sandbox Code Playgroud)

到你的Web.Config,我做了但没有帮助.有趣的是,Web服务可以正常使用SOAP或发送查询字符串,但不能使用JSON.

有任何想法吗?

Ann*_*lle 8

您需要将输入data作为JSON字符串提供给属性,而不是作为对象:

$(document).ready(function() {
  $.ajax({
    type: "POST",
    url: "http://localhost:63056/Service1.asmx/PrintOrderRecieptXml",
    data: '{"CouponCode":"TESTCODE","Subtotal":14.2600,"ShippingTotal":7.5000,"TaxTotal":0.0000,"GrandTotal":21.7600,"OrderItemCollection":[{"Total":14.2600,"Qty":250}]}',
    dataType: "json",
    contentType: "application/json",
    error: function(xhr, msg) { alert(xhr.statusText); }
});});
Run Code Online (Sandbox Code Playgroud)

使用jQuery消费ASP.NET JSON Web服务在与ASP.Net Web服务交谈时对需求有很好的解释.