Jquery调用REST自托管端口号

时间:2012-03-13 19:24:59

标签: jquery wcf-rest

我只是尝试在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(object sender,EventArgs e)         {             的RegisterRoutes();         }

    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;
            }

        });
    });
我真的应该放什么?特别是我很难找到我的hostlocal上的端口。 我试过http://localhost:5187/KendoRestService/GetCollection,其中KendoRestService是REST项目的名称,以及service.cs上的命名空间。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我相信你想给[WebGet(UriTemplate =“”)]一个值,即[WebGet(UriTemplate =“getCollection”)]

然后javascript网址将是:

var serviceUrl = ServiceName/UriTemplate

所以在你的情况下:

var serviceUrl = '/getCollection';

答案 1 :(得分:0)

好的,我找到了端口号。检查服务项目的属性Alt + Enter,Web选项卡,在服务器下检查将服务器设置应用于所有用户,以及使用Visual Studio开发服务器,自动分配端口。特定端口选项前面有一个端口号。 Visual Studio将使用此端口号。

相关问题