【发布时间】: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