views:

884

answers:

1

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

+1  A: 

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
Not sure what you mean. The GetTestString has the WebInvoke attribute with the GET option
DotnetDude
Edited my answer for more detail (since the code block won't sit in a comment nicely).
Dan Puzey
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
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
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
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
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
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
Oh, and regarding validation of the incorrect verb: you got a 405 error response - that's your validation working ;-)
Dan Puzey