I’m making a GET request against a 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 properly as I can make a bare call within 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 via navigation in my browser returns:
{"GetMessageResult":"Hello World!"}
So far so good. No problems here. A quick look at the jQuery documentation for performing a GET yields:
<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 in 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 failing?
- Why is the error message useless?
Solution
As it turns out, jQuery does not natively 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 a webHttpBinding with the crossDomainScriptEnabled property enabled:
<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 get errors unless you configure your WCF service to allow cross domain scripts.
Are you making a cross-domain request? I see you are using localhost, but that doesn’t necessarily mean you are requesting from localhost (for all we know you could be using a different port or protocol).
There are two reasons that the request would be failing in this case:
I expect it to be #1 due to you not seeing the
same-originerror in the console. However, since you’re gettingerrorand notparseerror, that is unlikely.