I am making a GET request to the WCF web service. My WCF service is located at http://localhost/RestService/RestService.svc/web/GetMessage and has the following interface:
[OperationContract] [WebGet(UriTemplate = "GetMessage", ResponseFormat = WebMessageFormat.Json)] String GetMessage();
The endpoint is configured correctly, since I can make a bare call in my browser:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="WebServiceBehavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="WebEndpointBehavior"> <webHttp /> </behavior> </endpointBehaviors> </behaviors> <services> <service name="TestRestService.RestService" behaviorConfiguration="WebServiceBehavior"> <endpoint name="RestWeb" address="web" binding="webHttpBinding" behaviorConfiguration="WebEndpointBehavior" contract="TestRestService.IRestService" /> </service> </services> </system.serviceModel>
Calling it using navigation in my browser returns:
{"GetMessageResult":"Hello World!"}
So far so good. There is no problem. A quick look at jQuery documentation for executing GET gives:
<html> <head> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> $.ajax({ url: 'http://localhost/RestService/RestService.svc/web/GetMessage', type: 'GET', dataType: 'json', success: function (data) { alert(data); }, error: function (xhr, status, message) { alert("Error: " + status + " " + message); } }); </script> </head> <body> </body> </html>
I run this on a small html test page using jQuery 1.72 and I get the following error:
Error: error
What gives? The error message handler that I found here gives me absolutely zero useful information. Simply put:
- Why is my GET not working?
- Why is the error message useless?
Decision
As it turns out, jQuery does not support cross-domain ajax requests, as Kevin B suggested in his answer . To fix this, I had to switch to using dataType: 'jsonp' and add webHttpBinding with the crossDomainScriptEnabled property crossDomainScriptEnabled :
<bindings> <webHttpBinding> <binding name="WebBindingWithScripts" crossDomainScriptAccessEnabled="true"> <security mode="None" /> </binding> </webHttpBinding> </bindings> <endpoint name="RestWeb" address="web" binding="webHttpBinding" behaviorConfiguration="WebEndpointBEhavior" bindingConfiguration="WebBindingWithScripts" contract="TestService.IRestService"> </endpoint>
When using only dataType: 'jsonp' you will still receive errors if you do not configure your WCF service to allow cross-domain domain scripts.