ASP.NET Web 服务和 JQuery 客户端的问题
我错过了什么吗?我正在尝试使用 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/">Hello World</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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,我在这里找到了答案: http://forums.asp.net/ p/1054378/2338982.aspx#2338982
不确定这个人在哪里找到答案,但你必须将其添加到 web.config 中:
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: