让它与 Visual Studio 2005 和 .NET 2.0 一起工作可能有点棘手,尤其是因为 Internet 上的很多信息都参考了 .NET 3.5,它默认包含 AJAX 组件。正如Aaonaught 提到的,您首先需要安装ASP.NET AJAX Extensions for .NET 2.0。
安装 AJAX 扩展后,您需要添加一个新的“启用 AJAX”网站:转到文件 > 新建 > 网站。选择“启用 ASP.NET AJAX 的网站”。这将具有与 .NET 2.0 中的常规 ASP.NET 站点不同的配置文件,因此选择这种站点很重要。
接下来,如果您的网络配置中尚未引用它,您需要右键单击您的项目并转到“添加引用”。添加对 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>
/// Summary description for StockQuote
/// </summary>
[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
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>