C# Web Service 不会输出 JSON,只会输出 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/663791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Web Service won't output JSON, only XML
提问by TheDude
I'm trying to use jQuery and JSON with a C# Web Service that I wrote. No matter what, the following code will only output in XML.
我正在尝试将 jQuery 和 JSON 与我编写的 C# Web 服务一起使用。不管怎样,下面的代码只会以XML格式输出。
Webservice Code
网络服务代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld() {
return "Hello World!";
}
I also have these attributes assigned to the class
我也将这些属性分配给了班级
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
jQuery Code
jQuery 代码
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ScheduleComputerDS.asmx/HelloWorld",
data: "{}",
dataType: "jsonp",
success: function(data) {
alert(data);
}
});
The ASMX page always returns as content type "text/xml". Anything I'm missing?
ASMX 页面始终以内容类型“text/xml”的形式返回。我缺少什么吗?
EDITS:In response to a couple answers:
编辑:回应几个答案:
If I have the datatype as just "json" the content is still XML and jQuery also will not call my callback function. If I add the "&callback=?" to the url, IIS throws a HTTP 500 error.
如果我的数据类型只是“json”,则内容仍然是 XML,jQuery 也不会调用我的回调函数。如果我添加“&callback=?” 到 url,IIS 会抛出 HTTP 500 错误。
My class does inherit from "System.Web.Services.WebService".
我的班级确实继承自“System.Web.Services.WebService”。
From doing some research on your guys answers, it looks like I do need to mess with WCF. Unfortunately the JSON that is returned is more designed for MS Ajax and is a lot of useless bloat for my use. I may look into an open source library like Jayrock or something similar.
通过对你们的答案进行一些研究,看起来我确实需要弄乱 WCF。不幸的是,返回的 JSON 更像是为 MS Ajax 设计的,对我来说是很多无用的膨胀。我可能会研究像 Jayrock 这样的开源库或类似的东西。
Thanks for all your help!
感谢你的帮助!
采纳答案by Rob Windsor
As far as I know, the ScriptService attribute just allows the service to automatically create a JavaScript proxy (by appending /js to the endpoint address - ScheduleComputerDS.asmx/js in your case). It does not allow you to call the operations on the service the way you're trying to do.
据我所知,ScriptService 属性只允许服务自动创建一个 JavaScript 代理(通过将 /js 附加到端点地址 - ScheduleComputerDS.asmx/js 在您的情况下)。它不允许您按照您尝试的方式调用服务上的操作。
You could instead use a RESTful WCF service (which requires .NET 3.5) which you can access by sending a properly shaped URI via an HTTP GET.
您可以改为使用 RESTful WCF 服务(需要 .NET 3.5),您可以通过 HTTP GET 发送正确形状的 URI 来访问该服务。
回答by Russ Cam
Have you tried with datatype json
?
您是否尝试过使用数据类型json
?
Also, have a look at Encosia's Using jQuery to Consume ASP.NET JSON Web Servicesarticle on the matter. There's some good info on common pitfalls too.
此外,请查看 Encosia 的Using jQuery to Consume ASP.NET JSON Web Services文章。还有一些关于常见陷阱的好信息。
回答by eduncan911
I think there's a typo:
我认为有一个错字:
dataType: "jsonp",
Should be:
应该:
dataType: "json",
回答by bendewey
Rich Strahl has a really basic post that should help you out with this.
Rich Strahl 有一个非常基本的帖子,应该可以帮助你解决这个问题。