Reputation: 1661
I've been searching long for an answer (on SO too; I know this is not a properly "fresh" question) but I did not find a solution to my problem yet. I have a WCF REST service, defined as follows:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "json/getUserOperations")]
Response<List<Operation>> GetUserOperations();
with this 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>
This works well if called from browser. But if I call this in jquery ajax, as follows:
$.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'); }
});
the service returns me a "405 Method not allowed" message. These are the headers of the request and of the response message:
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
So I fell into this, finding out that it could be some issue related to cross-domain calls, and then I modified my OperationContract as follows:
[OperationContract]
[WebInvoke(Method = "*",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "json/getUserOperations")]
Response<List<Operation>> GetUserOperations();
In this way, the request seems to be correctly served, as the headers show:
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
The response message actually contains the valid json data which I requested. By the way, the jquery.ajax function still trigger the error event... I don't understand why, as the response seems correctly received. Does anyone notice something wrong?
Regards!
Upvotes: 1
Views: 4249
Reputation: 1661
The solution is here: http://www.codeproject.com/KB/ajax/jQueryWCFRest.aspx
I've done as explained in "The cross-domain issue" paragraph and all works.
Upvotes: 1
Reputation: 7876
For jquery ajax calls i guess you need to have the below endpoint behaviour configuration:
<endpointBehaviors>
<behavior name="web">
<enableWebScript />
</behavior>
</endpointBehaviors>
Upvotes: 0
Reputation: 3054
I had a similar problem when I was implementing my Rest WCF service and it looks like you had everything I had except:
[WebGet(UriTemplate = "/GuessWhat/{variable}", ResponseFormat = WebMessageFormat.Json)]
I use that instead of the WebInvoke attribute. I use WebInvoke if I want to do an HTTP Post.
I am not sure if that will work for you, but that seemed to fix my problem.
Upvotes: 0