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

时间:2022-05-05 08:09:10

Here's an interesting problem:

这是一个有趣的问题:

I have some jQuery that looks like this:

我有一些看起来像这样的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); }
    });});

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.

现在,我遇到的问题是它正在发送请求,但Web服务没有正确处理它。在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):

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

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

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

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>

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.

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

Any ideas?

3 个解决方案

#1


8  

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

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

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

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 JSON Web服务在与ASP.Net Web服务交谈时能够很好地解释这些要求。

#2


3  

Douglas is correct - you need to format the data as a string. Be sure to read all of the posts on the blog that he linked you to. Encosia is a great resource for Ajax and Asp.Net.

道格拉斯是正确的 - 您需要将数据格式化为字符串。请务必阅读他链接到您的博客上的所有帖子。 Encosia是Ajax和Asp.Net的绝佳资源。

#3


1  

asp.net webservices don't return json normally. take a look here: JSON WebService in ASP.NET

asp.net webservices不会正常返回json。看一下:ASP.NET中的JSON WebService

#1


8  

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

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

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

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 JSON Web服务在与ASP.Net Web服务交谈时能够很好地解释这些要求。

#2


3  

Douglas is correct - you need to format the data as a string. Be sure to read all of the posts on the blog that he linked you to. Encosia is a great resource for Ajax and Asp.Net.

道格拉斯是正确的 - 您需要将数据格式化为字符串。请务必阅读他链接到您的博客上的所有帖子。 Encosia是Ajax和Asp.Net的绝佳资源。

#3


1  

asp.net webservices don't return json normally. take a look here: JSON WebService in ASP.NET

asp.net webservices不会正常返回json。看一下:ASP.NET中的JSON WebService