Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue when consuming a WCF REST service via jquery ajax call
    primarykey
    data
    text
    <p>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:</p> <pre><code> [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json/getUserOperations")] Response&lt;List&lt;Operation&gt;&gt; GetUserOperations(); </code></pre> <p>with this web.config:</p> <pre><code>&lt;system.serviceModel&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior name="WS_REST.DataServiceBehavior"&gt; &lt;serviceMetadata httpGetEnabled="true" /&gt; &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="web"&gt; &lt;webHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; &lt;services&gt; &lt;service behaviorConfiguration="WS_REST.DataServiceBehavior" name="WS_REST.DataService"&gt; &lt;endpoint address="" binding="webHttpBinding" contract="WS_REST.IDataService" behaviorConfiguration="web"&gt; &lt;identity&gt; &lt;dns value="localhost" /&gt; &lt;/identity&gt; &lt;/endpoint&gt; &lt;/service&gt; &lt;/services&gt; &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"/&gt; &lt;/system.serviceModel&gt; </code></pre> <p>This works well if called from browser. But if I call this in jquery ajax, as follows:</p> <pre><code>$.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'); } }); </code></pre> <p>the service returns me a "405 Method not allowed" message. These are the headers of the request and of the response message:</p> <pre><code> 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 </code></pre> <p>So I fell into <a href="https://stackoverflow.com/questions/4875195/problem-sending-json-data-from-jquery-to-wcf-rest-method">this</a>, finding out that it could be some issue related to cross-domain calls, and then I modified my OperationContract as follows:</p> <pre><code> [OperationContract] [WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "json/getUserOperations")] Response&lt;List&lt;Operation&gt;&gt; GetUserOperations(); </code></pre> <p>In this way, the request seems to be correctly served, as the headers show:</p> <pre><code> 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 </code></pre> <p>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? </p> <p>Regards!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload