开发者

405 Method Not Allowed Error in WCF

开发者 https://www.devze.com 2022-12-22 15:41 出处:网络
Can someone spot the problem with this implementation? I ca开发者_Go百科n open it up in the browser and it works, but a call from client side (using both jquery and asp.net ajax fails)

Can someone spot the problem with this implementation? I ca开发者_Go百科n open it up in the browser and it works, but a call from client side (using both jquery and asp.net ajax fails)

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);

I am then using the approach in http://www.west-wind.com/weblog/posts/324917.aspx to call the service


The example on your link uses a Http POST, not a Http GET. That's the "method [that's] not allowed" - you need to change the code to do a GET instead.

The link you post that was your source for 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

Note the type: "POST" in there - yours would need to be "GET". I'm assuming you've taken your JQuery from the link you posted, because the 405 status suggests that your calling code is wrong, not the service.


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

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

SHOULD BE IDENTICAL TO WHAT WAS SPECIFIED IN THE SERVICE INTERFACE (UNDER "[Operation Contract]")

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

精彩评论

暂无评论...
验证码 换一张
取 消