【发布时间】:2012-03-30 05:53:30
【问题描述】:
我只是尝试在 VS 2010 上制作一个解决方案来练习使用 JQuery 使用 REST 服务。 我使用了模板 WCF REST 服务应用程序。它创建了一个我命名为 Services.cs 的类 使用我想调用的方法:
[WebGet(UriTemplate = "")]
public List<SampleItem> GetCollection()
{
SampleItem item;
List<SampleItem> lst = new List<SampleItem>();
for (int i = 0; i < 12; i++)
{
item = new SampleItem { Prom = 1960 + i, Name = string.Format("Name {0}", i.ToString()) };
lst.Add(item);
}
return lst;
}
Global.asax.cs 看起来像: void Application_Start(对象发送者,EventArgs e) { 注册路由(); }
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service)));
}
然后我新建了一个 asp.net 空网站,添加了 jquery 文件等。在 default.htm 页面中我喜欢调用 GetCollection,所以我添加了以下 jquery 代码。
$(function () {
var serviceUrl = ??????
$.ajax({
url: serviceUrl,
type: 'POST',
data: '{}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (colD) {
var col = colD.d;
}
});
});
我应该放什么网址?特别是我很难弄清楚我的主机本地端口。 我试过http://localhost:5187/KendoRestService/GetCollection,其中 KendoRestService 是 REST 项目的名称,以及 service.cs 上的命名空间。任何帮助将不胜感激。
【问题讨论】: