通过jquery ajax调用使用WCF REST服务时出现问题

时间:2011-11-14 17:02:23

标签: jquery asp.net ajax wcf rest

我一直在寻找答案(也是如此;我知道这不是一个正确的“新鲜”问题),但我还没有找到解决问题的方法。我有一个WCF REST服务,定义如下:

    [OperationContract]
    [WebInvoke(Method = "GET",
       ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Bare,
       UriTemplate = "json/getUserOperations")]
Response<List<Operation>> GetUserOperations();

使用此web.config:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WS_REST.DataServiceBehavior">
                 <serviceMetadata httpGetEnabled="true" />
                 <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="web">
                <webHttp/>     
            </behavior>      
        </endpointBehaviors>
    </behaviors>    
    <services>
        <service behaviorConfiguration="WS_REST.DataServiceBehavior" name="WS_REST.DataService">
            <endpoint address="" binding="webHttpBinding" contract="WS_REST.IDataService" behaviorConfiguration="web">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

如果从浏览器调用,这很有效。但是如果我在jquery ajax中调用它,如下所示:

$.ajax({
        url: "http://localhost:1996/DataService.svc/json/getUserOperations",
        type: "GET",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        processdata: true,
        success: processReceivedData,
        error: onError,
        complete: function () { alert('completed'); }

 });

该服务返回“405 Method not allowed”消息。这些是请求和响应消息的标题:

    OPTIONS http://localhost:1996/DataService.svc/json/getUserOperations HTTP/1.1
    Host: localhost:1996
    User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Connection: keep-alive
    Origin: http://localhost:2100
    Access-Control-Request-Method: GET
    Access-Control-Request-Headers: content-type

    HTTP/1.1 405 Method Not Allowed
    Server: ASP.NET Development Server/9.0.0.0
    Date: Mon, 14 Nov 2011 16:28:58 GMT
    X-AspNet-Version: 2.0.50727
    Set-Cookie: ASP.NET_SessionId=0bd0cdyhwyvmuauqcbasvp45; path=/; HttpOnly
    Cache-Control: private
    Content-Type: text/html; charset=UTF-8
    Content-Length: 1024
    Connection: Close

所以我陷入this,发现它可能是与跨域调用相关的一些问题,然后我按如下方式修改了我的OperationContract:

    [OperationContract]
    [WebInvoke(Method = "*",
       ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Bare,
       UriTemplate = "json/getUserOperations")]
Response<List<Operation>> GetUserOperations();

通过这种方式,请求似乎正确提供,如标题所示:

    OPTIONS http://localhost:1996/DataService.svc/json/getUserOperations HTTP/1.1
    Host: localhost:1996
    User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Connection: keep-alive
    Origin: http://localhost:2100
    Access-Control-Request-Method: GET
    Access-Control-Request-Headers: content-type

    HTTP/1.1 200 OK
    Server: ASP.NET Development Server/9.0.0.0
    Date: Mon, 14 Nov 2011 16:45:19 GMT
    X-AspNet-Version: 2.0.50727
    Set-Cookie: ASP.NET_SessionId=as4tch55yulzc32fzxsefh2y; path=/; HttpOnly
    Cache-Control: private
    Content-Type: application/json; charset=utf-8
    Content-Length: 793
    Connection: Close

响应消息实际上包含我请求的有效json数据。顺便说一句,jquery.ajax函数仍然触发错误事件...我不明白为什么,因为响应似乎正确接收。 有人注意到了什么问题吗?

问候!

3 个答案:

答案 0 :(得分:1)

解决方案在这里:http://www.codeproject.com/KB/ajax/jQueryWCFRest.aspx

我按照“跨域问题”段落中的说明完成了所有工作。

答案 1 :(得分:0)

当我实现我的Rest WCF服务时,我遇到了类似的问题,看起来你拥有我所拥有的一切,除了:

[WebGet(UriTemplate = "/GuessWhat/{variable}", ResponseFormat = WebMessageFormat.Json)]

我使用它而不是WebInvoke属性。如果我想做一个HTTP Post,我会使用WebInvoke。

我不确定这是否适合你,但这似乎解决了我的问题。

答案 2 :(得分:0)

对于jquery ajax调用,我猜你需要具有以下端点行为配置:

<endpointBehaviors>
         <behavior name="web">
             <enableWebScript />
         </behavior>
</endpointBehaviors>