4

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

DotnetDude
  • 11,617
  • 35
  • 100
  • 158

2 Answers2

6

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.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • Not sure what you mean. The GetTestString has the WebInvoke attribute with the GET option – DotnetDude Mar 12 '10 at 22:02
  • Edited my answer for more detail (since the code block won't sit in a comment nicely). – Dan Puzey Mar 12 '10 at 22:55
  • Thanks! When I changed from POST to GET in the proxy JS, it started working. Do you know why the author chose to use POST when getting info from the service (I assume it should be a POST) – DotnetDude Mar 15 '10 at 13:15
  • 2
    Any web service can choose to implement any of the Http methods - the most common of which are `GET`, `POST`, `PUT` and `DELETE`. `POST` and `PUT` are normally used for writing information, and so in that respect the sample you linked is unusual - a method called `GetStockQuote` seems a strange choice to be implemented as a `POST` - but it's the service author's choice to make :-) It's worth noting that you *can* use any of the methods to return a result (you could use Http `DELETE` to return information if you wanted to!) - it just doesn't necessarily make good sense! – Dan Puzey Mar 15 '10 at 13:25
  • The important thing from the above probably being: it doesn't matter so much which Http method you use (except in terms of your service making sense). What's important is that if the service is a `POST` call, a client has to make a `POST` call for the service to work. Or, in your case, a `GET` :-) – Dan Puzey Mar 15 '10 at 13:27
  • Got it. Thanks! You point out that "hat you can use any of the methods to return a result". This is very interesting. I guess there is no validation of any kind when you make a call with the incorrect verb. – DotnetDude Mar 15 '10 at 13:57
  • I just found out that I get a prompt "This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?" whenever the client tries to make a AJAX call in IE. In FF, I continue to get the "405 Method not allowed" error – DotnetDude Mar 15 '10 at 14:08
  • The IE error sounds like maybe you're making a request from a webservice on a different (untrusted) domain to the site... The repeated 405 in FF could be a caching issue (if it's not picked up your updated javascript). – Dan Puzey Mar 15 '10 at 16:04
  • Oh, and regarding validation of the incorrect verb: you got a 405 error response - that's your validation working ;-) – Dan Puzey Mar 15 '10 at 16:05
  • 1
    I wish that I could give a million up votes for this. I feel so stupid for missing this. – broguyman Sep 27 '12 at 19:43
1

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)]
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75