有没有办法让VS 2005中创建的ASMX Web服务接收并返回JSON?
我正在使用.NET 2.0和Visual Studio 2005来尝试创建一个可以作为SOAP / XML和JSON使用的Web服务。 我读了Dave Ward对如何从2.0 asmx Web服务返回JSON的问题的答案 (除了阅读Encosia.com上的其他文章),但我无法弄清楚我需要如何设置我的asmx文件的代码为了使用jQuery使用JSON。
两个问题:
- 如何在我的.NET 2.0 ASMX文件中启用JSON?
- 什么是可以使用JSON使用服务的简单jQuery调用?
另外,我注意到因为我使用的是.NET 2.0,所以我无法using System.Web.Script.Services.ScriptService
实现。
这是我的演示ASMX服务的C#代码:
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; /// /// Summary description for StockQuote /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class StockQuote : System.Web.Services.WebService { public StockQuote () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public decimal GetStockQuote(string ticker) { //perform database lookup here return 8; } [WebMethod] public string HelloWorld() { return "Hello World"; } }
这是我在互联网上找到的一个jQuery片段,试图修改:
$(document).ready(function(){ $("#btnSubmit").click(function(event){ $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "http://bmccorm-xp/WebServices/HelloWorld.asmx", data: "", dataType: "json" }) event.preventDefault(); }); });
您可以将ScriptService
与.NET 2.0 / Visual Studio 2005一起使用。您只需安装.NET 3.5之前发布的ASP.NET AJAX Extensions ,它就会成为框架的核心组件。
使用Visual Studio 2005和.NET 2.0可能会有点棘手,特别是因为互联网上的大量信息引用了.NET 3.5,默认情况下包括AJAX组件。 正如Aaonaught所说,您首先需要安装ASP.NET AJAX Extensions for .NET 2.0 。
安装AJAX扩展后,您将需要添加一个新的“AJAX Enabled”网站:转到文件>新建>网站。 选择“ASP.NET AJAX Enabled Website”。 这将具有与.NET 2.0中的常规ASP.NET站点不同的配置文件,因此选择此类站点非常重要。
接下来,如果您的Web配置中尚未引用它,则需要右键单击您的项目并转到“添加引用”。 添加对System.Web.Extensions
版本1.0.61025.0的引用。 这是新脚本库所在的位置( 更新:我可以确认,如果您在VS 2005中将项目设置为“支持AJAX的网站”,它将自动在Web.Config文件中包含对此程序集的引用 )。
最后两个步骤将允许您在代码中添加对System.Web.Script.Services.ScriptService
的引用。 现在,您可以将.asmx Web服务添加到项目中,您需要做的就是在服务类之前添加以下属性: [System.Web.Script.Services.ScriptService]
。 您的代码应该类似于:
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; /// /// Summary description for StockQuote /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class StockQuote : System.Web.Services.WebService { public StockQuote () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public decimal GetStockQuote(string ticker) { //perform database lookup here return 8; } [WebMethod] public string HelloWorld() { return "Hello World"; } }
您的示例代码的部分问题在于您没有调用您在示例中定义的相同Web服务。 你曾经从jQuery调用过HelloWorld.asmx
,但你应该调用StockQuote.asmx/HelloWorld
。 现在,当您使用application/json
作为内容类型调用jQuery方法时,Web服务将遵循并使用JSON而不是XML进行响应。
JSON POST
POST http://bmccorm-xp/WebServicesAjax/StockQuote.asmx/HelloWorld HTTP/1.1 x-requested-with: XMLHttpRequest Accept-Language: en-us Referer: http://bmccorm-xp/WebServicesAjax/TestJSON.html Accept: application/json, text/javascript, */* Content-Type: application/json; charset=utf-8 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) Host: bmccorm-xp Content-Length: 0 Connection: Keep-Alive Pragma: no-cache
JSON 响应 :
HTTP/1.1 200 OK Server: Microsoft-IIS/5.1 Date: Tue, 27 Apr 2010 19:11:40 GMT X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Cache-Control: private, max-age=0 Content-Type: application/json; charset=utf-8 Content-Length: 13 "Hello World"
jQuery POST ,要求XML:
POST http://bmccorm-xp/WebServicesAjax/StockQuote.asmx/HelloWorld HTTP/1.1 x-requested-with: XMLHttpRequest Accept-Language: en-us Referer: http://bmccorm-xp/WebServicesAjax/TestJSON.html Accept: application/json, text/javascript, */* Content-Type: text/xml; charset=utf-8 Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) Host: bmccorm-xp Content-Length: 0 Connection: Keep-Alive Pragma: no-cache
XML 响应 :
HTTP/1.1 200 OK Server: Microsoft-IIS/5.1 Date: Tue, 27 Apr 2010 18:54:55 GMT X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Cache-Control: private, max-age=0 Content-Type: text/xml; charset=utf-8 Content-Length: 96 Hello World