如何从3.5 asmx Web服务获取JSON响应

我有以下方法:

using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using Newtonsoft.Json; using System.Collections; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] //[System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] // [System.Web.Script.Services.ScriptService] public class Tripadvisor : System.Web.Services.WebService { public Tripadvisor () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string HotelAvailability(string api) { JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(api); //JsonConvert.SerializeObject(api); return json ; } 

在这里我设置ResponseFormat属性是json仍然作为XML返回。

我想用json格式使用这个asmx服务有什么想法吗?

我遇到了同样的问题,并包含以下代码以使其工作。

 [WebMethod] [ScriptMethod(UseHttpGet=true ,ResponseFormat = ResponseFormat.Json)] public void HelloWorld() { Context.Response.Clear(); Context.Response.ContentType = "application/json"; Context.Response.Write("Hello World"); //return "Hello World"; } 

更新:

要获得纯json格式,您可以使用如下所示的javascript序列化程序。

 public class WebService1 : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet=true ,ResponseFormat = ResponseFormat.Json)] public void HelloWorld() { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; HelloWorldData data = new HelloWorldData(); data.Message = "HelloWorld"; Context.Response.Write(js.Serialize(data)); } } public class HelloWorldData { public String Message; } 

但是这适用于复杂类型,但字符串不会显示任何差异。

只是一个疑问。 你什么时候没有得到JSON回复? 因为当您从客户端调用Web服务时(我假设是Web浏览器,即xhr),您应该在请求中将内容类型标题指定为“application / json; charset = yourcharset”。

我相信无论从客户端指定什么内容类型,上述解决方案总是返回json。 dotnet asmx框架允许使用内容类型头方法,因此当一个简洁的解决方案可用时,上面的内容可以归类为hack。

来自ASMX Web服务的Return Json Data的类似问题

这也许有帮助 – > http://forums.asp.net/p/1054378/2338982.aspx#2338982

PS:我假设您使用的是dotnet版本4。

亲爱的未来读者:目前接受的答案不是正确的方法。 它将您绑定到使用JavaScriptSerializer并且您失去了请求xml的能力(或者实际上可能会出现的任何序列化格式)。 “正确的方式”也涉及更少的代码!

如果使用[ScriptService]属性(您拥有)来装饰服务类,那么只要您的Ajax调用请求JSON, ASP.NET 3.5+就会自动将响应序列化为 JSON 。 手动串行化为JSON的建议完全错误,除非您希望使用其他序列化程序,如Newtonsoft。

你看到XML表明以下之一:

  • 您没有在Ajax调用中请求JSON – 请参阅下面的工作示例代码
  • 根据这里的答案,可能缺少一些web.config条目(免责声明:我在生产web.config中没有大部分这些条目;只有在没有其他工作的情况下才开始使用这些条目)

以下是启用JSON的ASMX Web服务的简单工作示例:

 <%@ WebService Language="C#" Class="WebService" %> using System; using System.Collections.Generic; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] public MyClass Example() { return new MyClass(); } public class MyClass { public string Message { get { return "Hi"; } } public int Number { get { return 123; } } public List List { get { return new List { "Item1", "Item2", "Item3" }; } } } } 

JavaScript请求它并处理响应(我们只需使用来自MyClass.Message的消息弹出JS警报):

   Test       

Http请求:

 POST http://HOST.com/WebService.asmx/Example HTTP/1.1 Accept: application/json, text/javascript, */*; q=0.01 Content-Type: application/json; charset=utf-8 X-Requested-With: XMLHttpRequest Referer: http://HOST.com/Test.aspx Accept-Language: en-GB,en;q=0.5 Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0) Connection: Keep-Alive Content-Length: 3 Host: HOST.com { } 

HTTP响应:

 HTTP/1.1 200 OK Cache-Control: private, max-age=0 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 20 Feb 2018 08:36:12 GMT Content-Length: 98 {"d":{"__type":"WebService+MyClass","Message":"Hi","Number":123,"List":["Item1","Item2","Item3"]}} 

结果:

“Hi”显示在JS弹出窗口中。