JQuery get fails with no error message

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.

+4
source share
2 answers

Are you making a cross-domain request? I see that you are using localhost, but that doesn’t necessarily mean that you are requesting from localhost (for everyone we know, you can use a different port or protocol).

In this case, the request will fail:

  • the returned JSON has extra characters, making it invalid json
  • The request comes from a different domain than the web service.

I expect this to be # 1 due to the fact that you do not see the same-origin error in the console. However, since you are getting error , not parseerror , this is unlikely.

+2
source

It happened to me once. All I did was add endpointBehaviors to my web.config to support WebHttp requests like this.

  <behaviors> <serviceBehaviors> ...... </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="EndpBehavior"> <webHttp/> </behavior> </endpointBehaviors> 

and changed my endpoint by turning on behaviorConfiguration and changing the binding

  <endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="EndpBehavior"> 

Remember to add this to your IService:

 [WebGet(ResponseFormat =WebMessageFormat.Json)] 
+2
source

All Articles