无法使用jQuery的ASP.NET Web服务 [英] Can't consume ASP.NET Web Service from jQuery

查看:72
本文介绍了无法使用jQuery的ASP.NET Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的问题:

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

I have some jQuery that looks like this:

    $(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); }
    });});

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

Now, the problem I'm having is that it's sending the request, but the web service isn't processing it correctly. In IE, I get an alert box with "Internal Server Error" and with FireFox I get an alert box with nothing in it.

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

The strange thing is that when I use IE, I do not get an error event in my event log, but with firefox I get (bonus points for figuring out why this is):

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

"Exception message: Request format is unrecognized for URL unexpectedly ending in '/PrintOrderRecieptXml"

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

I poked around some and found out that sometimes you have to add:

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

  </protocols>
</webServices>

到您的Web.Config,我这样做了,但没有帮助.有趣的是,Web服务可以与SOAP或发送查询字符串一起正常工作,但不能与JSON一起工作.

To your Web.Config, which I did but it did not help. The interesting thing is that the web service works fine with SOAP or sending a query string, but not with JSON.

有什么想法吗?

推荐答案

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

You need to give your input to the data property as a JSON string, not as an object:

$(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); }
});});

使用jQuery进行消费当与ASP.Net Web服务进行通信时,ASP.NET JSON Web服务很好地解释了这些要求.

Using jQuery to Consume ASP.NET JSON Web Services has a good explanation of the requirements when talking to ASP.Net Web Services.

这篇关于无法使用jQuery的ASP.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆