简体   繁体   English

C# Web Service won't output JSON, only XML

[英]C# Web Service won't output JSON, only XML

I'm trying to use jQuery and JSON with a C# Web Service that I wrote.我正在尝试将 jQuery 和 JSON 与 C# jQuery 和 JSON 一起使用。 No matter what, the following code will only output in XML.不管怎样,下面的代码只会 XML 中的 output。

Webservice Code网络服务代码

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

I also have these attributes assigned to the class我还将这些属性分配给 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". ASMX 页面始终以内容类型“text/xml”的形式返回。 Anything I'm missing?有什么我想念的吗?

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.如果我的数据类型只是“json”,则内容仍然是 XML 和 jQuery 也不会调用我的回调 function。 If I add the "&callback=?"如果我添加“&callback=?” to the url, IIS throws a HTTP 500 error.对于 url,IIS 会引发 HTTP 500 错误。

My class does inherit from "System.Web.Services.WebService".我的 class 确实继承自“System.Web.Services.WebService”。

From doing some research on your guys answers, it looks like I do need to mess with WCF.通过对你们的答案进行一些研究,看来我确实需要弄乱 WCF。 Unfortunately the JSON that is returned is more designed for MS Ajax and is a lot of useless bloat for my use.不幸的是,返回的 JSON 更多地是为 MS Ajax 设计的,对我来说是很多无用的膨胀。 I may look into an open source library like Jayrock or something similar.我可能会研究像 Jayrock 或类似的开源库。

Thanks for all your help!感谢你的帮助!

I think there's a typo:我认为有一个错字:

dataType: "jsonp",

Should be:应该:

dataType: "json",

Rich Strahl has a really basic post that should help you out with this. Rich Strahl 有一个非常基本的帖子,应该可以帮助您解决这个问题。

http://www.west-wind.com/weblog/posts/164419.aspx http://www.west-wind.com/weblog/posts/164419.aspx

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).据我所知, ScriptService 属性只允许服务自动创建 JavaScript 代理(通过将 /js 附加到端点地址 - 在您的情况下为 ScheduleComputerDS.asmx/js )。 It does not allow you to call the operations on the service the way you're trying to do.它不允许您以您尝试的方式调用服务上的操作。

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 来访问该服务。

Have you tried with datatype json ?您是否尝试过使用数据类型json

Also, have a look at Encosia's Using jQuery to Consume ASP.NET JSON Web Services article on the matter.另外,请查看 Encosia 的Using jQuery to Consume ASP.NET JSON ZC6E190B284633C48EZ3 文章 584633C48EZ3 服务 There's some good info on common pitfalls too.也有一些关于常见陷阱的好信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM