Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

asp.net - 405 Method Not Allowed Error in WCF

Can someone spot the problem with this implementation? I can 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...