Note that there are some explanatory texts on larger screens.

plurals
  1. PO400 Bad Request JQuery WCF Service
    primarykey
    data
    text
    <p>I created a WCF web service in ASP.NET 4.5 in VS2012 that returns a JSON response. The service works fine with the built in webservice client and I got the endpoints set up correctly in the Web.config file.</p> <p>On the client side, I have a simple test script in Jquery, however, after running the script, I get a HTTP/1.1 400 Bad Request.</p> <p>The web service requires no input at all - so data: is set to an empty string.</p> <p>Here is what I get when calling the service (using HTTP Live Headers add-on in FireFox).</p> <pre><code>http://localhost:58234/CCSVC.svc/Get_BTCE_BTC_USD OPTIONS /CCSVC.svc/Get_BTCE_BTC_USD HTTP/1.1 Host: localhost:58234 User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Origin: http://localhost:59099 Access-Control-Request-Method: GET Access-Control-Request-Headers: content-type Connection: keep-alive HTTP/1.1 400 Bad Request Cache-Control: private Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcc2l0ZXMyXENyeXB0b0NvaW5TZXJ2aWNlc1xDQ1NWQy5zdmNcR2V0X0JUQ0VfQlRDX1VTRA==?= X-Powered-By: ASP.NET Date: Mon, 23 Dec 2013 16:08:27 GMT Content-Length: 0 </code></pre> <p>And the script:</p> <pre><code>$(document).ready(function () { $('#btnRefresh').click(function () { $.ajax({ type: 'GET', url: 'http://localhost:58234/CCSVC.svc/Get_BTCE_BTC_USD', data: '', contentType: 'application/json', dataType: 'json', processData: true, crossDomain: true, success: function (msg) { ServiceSucceeded(msg); }, error: function (msg) { ServiceFailed(msg); } }); function ServiceSucceeded(result) { alert("success"); }; function ServiceFailed(result) { alert("fail"); }; }); }); </code></pre> <p>naturally, it fails. I've tried several different combinations with no luck and POST, as well as GET. crossDomain true/false, processData true/false, etc. nothing seems to work.</p> <p>Here's the CONTRACT for the webservice:</p> <pre><code>&lt;OperationContract()&gt; &lt;WebInvoke(BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)&gt; Function Get_BTCE_BTC_USD() As TradeData </code></pre> <p>This appears to be a problem with the request, but the error does not indicated what that might be. </p> <p>Here is the inside of the web.config in the WCF service:</p> <pre><code> &lt;configSections&gt; &lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" &gt; &lt;section name="CryptoCoinServices.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt; &lt;/sectionGroup&gt; &lt;/configSections&gt; &lt;appSettings&gt; &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt; &lt;/appSettings&gt; &lt;system.web&gt; &lt;compilation debug="true" strict="false" explicit="true" targetFramework="4.5" /&gt; &lt;httpRuntime targetFramework="4.5"/&gt; &lt;/system.web&gt; &lt;system.serviceModel&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior&gt; &lt;!-- To avoid disclosing metadata information, set the values below to false before deployment --&gt; &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt; &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --&gt; &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt; &lt;/behavior&gt; &lt;behavior name="metadataBehavior"&gt; &lt;serviceMetadata httpGetEnabled="true" /&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;services&gt; &lt;service behaviorConfiguration="metadataBehavior" name="CryptoCoinServices.Service1"&gt; &lt;!-- leave address empty for localhost --&gt; &lt;endpoint address="" binding="basicHttpBinding" contract="CryptoCoinServices.IService1" /&gt; &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt; &lt;/service&gt; &lt;/services&gt; &lt;protocolMapping&gt; &lt;add binding="basicHttpsBinding" scheme="https" /&gt; &lt;/protocolMapping&gt; &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt; &lt;/system.serviceModel&gt; &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"/&gt; &lt;!-- To browse web app root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web app folder information. --&gt; &lt;directoryBrowse enabled="true"/&gt; &lt;/system.webServer&gt; </code></pre> <p>Here are recommended changes in the jQuery script (still doesn't work, but the {} for the data is mandatory, apparently):</p> <pre><code> $.ajax({ type: 'POST', url: 'http://localhost:58234/CCSVC.svc/Get_BTCE_BTC_USD', data: '{}', contentType: 'application/json; charset=utf-8', dataType: 'json', processData: true, crossDomain: true, success: function (msg) { ServiceSucceeded(msg); }, error: function (msg) { ServiceFailed(msg); } }); </code></pre> <p>New Error after change to webHttpBinding:</p> <pre><code>http://localhost:58234/CCSVC.svc/Get_BTCE_BTC_USD OPTIONS /CCSVC.svc/Get_BTCE_BTC_USD HTTP/1.1 Host: localhost:58234 User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Origin: http://localhost:59099 Access-Control-Request-Method: POST Access-Control-Request-Headers: content-type Connection: keep-alive Pragma: no-cache Cache-Control: no-cache HTTP/1.1 500 Internal Server Error Cache-Control: private Content-Length: 513 Content-Type: application/xml; charset=utf-8 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RTpcc2l0ZXMyXENyeXB0b0NvaW5TZXJ2aWNlc1xDQ1NWQy5zdmNcR2V0X0JUQ0VfQlRDX1VTRA==?= X-Powered-By: ASP.NET Date: Mon, 23 Dec 2013 17:02:44 GMT </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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