ASP.NET Web 服务和 JQuery 客户端的问题

发布于 2024-08-19 15:34:04 字数 1338 浏览 4 评论 0原文

我错过了什么吗?我正在尝试使用 JSON 和 JQuery 在 asp.net 中创建 Web 服务和使用者,但我没有任何运气。我可以让 JQuery 调用服务,并让服务回复,但响应总是经过 JQuery 中的“错误”回调。当我在 FireBug 中查看响应时,它似乎是 XML,而不是 JSON。下面是我的服务和来自客户端的相关 JQuery。任何帮助将不胜感激:

<%@ WebService Language="C#" Class="ajaxService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class ajaxService: System.Web.Services.WebService {

 [WebMethod()]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public string HelloWorld() {
  return "Hello World";
 }
}

JQuery

$(document).ready(function() {
  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{}",
    url: "ajaxService.asmx/HelloWorld",
    success: function(msg) {
      alert("success " + msg.d);
    },
    error: function(err) {
      alert(err.status + " : " + err.statusText);
    }
  });
});

响应始终显示“OK : 200”,响应内容为:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/"&gt;Hello World&lt;/string>

Am I missing something? I am trying to create a web service and consumer in asp.net, using JSON with JQuery, but I'm not having any luck. I can get JQuery to call the service, and get the service to reply, but the response always goes through the "error" callback in JQuery. When I view the response in FireBug, it appears to be XML, not JSON. Below is my service and the relevant JQuery from the client. Any help would be appreciated:

<%@ WebService Language="C#" Class="ajaxService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class ajaxService: System.Web.Services.WebService {

 [WebMethod()]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public string HelloWorld() {
  return "Hello World";
 }
}

JQuery

$(document).ready(function() {
  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: "{}",
    url: "ajaxService.asmx/HelloWorld",
    success: function(msg) {
      alert("success " + msg.d);
    },
    error: function(err) {
      alert(err.status + " : " + err.statusText);
    }
  });
});

The response always states "OK : 200", and the response content is:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜深人未静 2024-08-26 15:34:05

实际上,我在这里找到了答案: http://forums.asp.net/ p/1054378/2338982.aspx#2338982

不确定这个人在哪里找到答案,但你必须将其添加到 web.config 中:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>

Actually, I found the answer here: http://forums.asp.net/p/1054378/2338982.aspx#2338982

Not sure where this guy found the answer, but you have to add this to the web.config:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文