Error 405 Error in WCF

Can someone identify a problem with this implementation? I can open it in a browser and it works, but a call from the client side (using both jquery and asp.net ajax)

Service contract

[OperationContract(Name = "GetTestString")] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json )] string GetTestString(); 

In Web.config, among other bindings, I have a webHttp binding

 <endpoint address="ajax" binding="webHttpBinding" contract="TestService" behaviorConfiguration="AjaxBehavior" /> 

EndPoint Behavior

  <endpointBehaviors> <behavior name="AjaxBehavior"> <enableWebScript/> </behavior> </endpointBehaviors> </behaviors> 

Svc file

 <%@ ServiceHost Service="TestService" %> 

Client

 var serviceUrl = "http://127.0.0.1/Test.svc/ajax/"; var proxy = new ServiceProxy(serviceUrl); 

Then I use the approach at http://www.west-wind.com/weblog/posts/324917.aspx to call the service

+4
source share
2 answers

In the example, your link uses Http POST, not Http GET. That "the method [this] is not allowed" - you need to change the code to execute GET instead.

The link that you publish, which is your source for the client code, has this block:

  $.ajax( { url: url, data: json, type: "POST", processData: false, contentType: "application/json", timeout: 10000, dataType: "text", // not "json" we'll parse 

Pay attention to type: "POST" where you will need to "GET". I assume you took jQuery from the link you posted, because 405 status means your calling code is wrong, not a service.

+6
source

for a method error is not allowed, all you need to check is to make sure that your HTTP request / request is the same as that specified in [WebInvoke ...] in the service

  $.ajax({ type: "POST",.....}); 

SHOULD BE IDENTICAL FOR WHICH IT IS SPECIFIED IN THE SERVICE INTERFACE (CONNECTED "[Operational Agreement]")

  [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)] 
+1
source

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


All Articles