【问题标题】:Issue when consuming a WCF REST service via jquery ajax call通过 jquery ajax 调用使用 WCF REST 服务时出现问题
【发布时间】:2011-11-14 17:02:22
【问题描述】:

我一直在寻找答案(在 SO 上也是如此;我知道这不是一个适当的“新鲜”问题),但我还没有找到解决问题的方法。我有一个 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 方法不允许”消息。这些是请求和响应消息的标头:

    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 函数仍然触发错误事件......我不明白为什么,因为响应似乎正确收到。 有没有人注意到有什么不对?

问候!

【问题讨论】:

  • 您的 javascript 显示“type: "GET"" 但标题显示 'OPTIONS'。这很奇怪。
  • 阅读“预检请求”段落; options 涉及跨域调用...

标签: jquery asp.net ajax wcf rest


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    我不确定这是否对你有用,但这似乎解决了我的问题。

    【讨论】:

    • 谢谢,但这并没有什么区别...顺便说一下,我认为 WebGet 与方法 = "GET" 的 WebInvoke 相同。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 2011-04-20
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多