How to call a simple WCF service using jQuery or plain js

I have a very simple WCF Welcome Service, as shown below. When I call it through the asp.net project, adding a link to the web service, it works fine. But when I call it using jQuery or a standard js-ajax call (using XMLHttpRequest ), it calls the success function, but returns null data.

When I tried to access it through the Firefox browser using this address: http://localhost:8282/Test/TestService.svc/HelloWorld

He returned an error with the code "a: ActionNotSupported" and the error detail as

A message with an action "cannot be processed at the receiver due to a ContractFilter mismatch in the EndpointDispatcher. This may be due to a contract mismatch (action mismatch between the sender and the recipient) or a binding / security mismatch between the sender and the recipient. Make sure the sender and recipient have the same contract and the same binding (including security requirements, such as message, transport, no).

If I change the binding to wsHttpBinding , then it does not return anything even in Firefox.

Here is the code:

File "Test / ITestService.svc":

 [ServiceContract(Namespace = "http://localhost:8282/")] public interface ITestService { [OperationContract] string HelloWorld(); } 

File "Test / TestService.svc":

 public class TestService : ITestService { public string HelloWorld() { return "This is echo from server. Hello World"; } } 

File "web.config"

 <system.serviceModel> <services> <service name="radMLRPC.Test.TestService" behaviorConfiguration="radMLRPC.Test.TestServiceBehavior" <endpoint address="HelloWorld" binding="webHttpBinding" contract="radMLRPC.Test.ITestService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="radMLRPC.Test.TestServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> 
+4
source share
2 answers

with the code above, the service only allows soap requests to allow us to receive HTTP requests, which we must change, as shown below:

In the interface:

  [WebGet(UriTemplate="helloworld")] [OperationContract] string HelloWorld(); 

in web configuration:

  • add behaviorConfiguration:

     <endpoint address="" binding="webHttpBinding" contract="radMLRPC.Test.ITestService" behaviorConfiguration="webBehav"> 
  • then in the behavior add the following tag:

    <endpointBehaviors> <behavior name = "webBehav"> <webHttp / "> </ behavior> </endpointBehaviors>

"remove extra spaces from above. He didn’t show tags without extra spaces"


Take a look at some resources for this:

Introduction to RESTful Services with WCF http://msdn.microsoft.com/en-us/magazine/dd315413.aspx

Endpoint.TV screencasts:

Endpoint.TV as a whole has really good coverage for WCF and WCF REST. http://channel9.msdn.com/shows/Endpoint/

+4
source

Consumption of ASP.net WebServices, WCF services and static page methods from JavaScript (without MS AJAX)

For webHttp and client script, mex binding is not useful. drop it now.

Identity can cause you some grief, throw it now.

You have HelloWorld as your address, to call the HelloWorld method on your service, you will need to call http: // localhost: 8282 / Test / TestService.svc / HelloWorld / HelloWorld . take it off.

 <services> <service name="radMLRPC.Test.TestService" behaviorConfiguration="radMLRPC.Test.TestServiceBehavior" <endpoint address="" binding="webHttpBinding" contract="radMLRPC.Test.ITestService"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="radMLRPC.Test.TestServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> 

Now, perhaps you will not get to the end, but give us a better starting point. I am ready to help you with this.

Compare what we have now with the working example shown in a related article.

 <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="AjaxEndpointBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="ClientScriptServices.Service1Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="ClientScriptServices.Service1Behavior" name="ClientScriptServices.Service1"> <endpoint behaviorConfiguration="AjaxEndpointBehavior" binding="webHttpBinding" contract="ClientScriptServices.Service1" /> </service> </services> </system.serviceModel> 

And see if we get the configuration in the form the same way, and we can cover some of the subtleties of customizing your input and output formats.

+1
source

Source: https://habr.com/ru/post/1316142/


All Articles