6

I have an OperationContract method where I am trying to query and insert data into the database. I am using a POST method and calling the service from javascript in the browser. The WCF Service is in the same domain, so I should not have to use JSONP. I am also modifying data so it should be a POST request not a GET request. However I am still getting a "Method not allowed error". Has anyone encountered this situation?

My service is being called at

http://some_url.com/services/ProfileService.svc/json/CurrentUser

Strangely, it seems to be called via a GET request when I go this url even though I specifiy a POST. On the page load, however, it seems to be attempting a POST request.

Browser response when going to the url:

Request URL:http://some_url.com/services/ProfileService.svc/json/CurrentUser
Request Method:GET
Status Code:405 Method Not Allowed
Request Headersview parsed
GET /services/ProfileService.svc/json/CurrentUser HTTP/1.1

Here is my method that I am trying to call:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]

public HIPUser GetCurrentUser()
{
    string[] domainUser;
    string Auth_User = HttpContext.Current.User.Identity.Name.ToString().ToLower();
    domainUser = Auth_User.Split('\\');
    string user = domainUser[1];
    Debug.WriteLine(user);

    ProfileManager pm = new ProfileManager();
    var results = pm.GetUserByUserName(user);

    if (results.Length > 0)
    {
        return results.First();
    }
    else
    {
        Debug.WriteLine("IS NULL");
        var x = pm.CreateUser(user, null, null);
        Debug.WriteLine(x.UserName);
        return x;
    }
}

Client:

function getCurrentUser() {

$.ajax({
    type: "POST",
    url: "services/ProfileService.svc/json/GetCurrentUser",
    contentType: "application/json; charset=utf-8",
    data: null,
    dataType: "json",
    error: function (request, error, u) {
        alert('blargherror: ' + error);
    },
    success: function (result, status) {
        alert(result.d);
    }
});
}

Not sure if needed but Web.Config:

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

    <serviceBehaviors>
        <behavior name="metaBehavior">
            <serviceDebug includeExceptionDetailInFaults="true" />
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>

<serviceHostingEnvironment 
    aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />

<services>
    <service name="ProfileService" behaviorConfiguration="metaBehavior">
        <endpoint address="/json" 
            behaviorConfiguration="jsonBehavior"
            binding="webHttpBinding" 
            bindingConfiguration="secure" 
            contract="ProfileService" />
        <endpoint address="" 
            binding="basicHttpBinding" 
            bindingConfiguration="secure" 
            contract="ProfileService" />
    </service>
</services>

Edit to Web.Config - Adding bindings

<bindings>
        <webHttpBinding>
            <binding name="secure">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows"/>
                </security>
            </binding>
        </webHttpBinding>
        <basicHttpBinding>
            <binding name="secure">
                <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
jocey
  • 252
  • 4
  • 14
  • 2
    This won't solve your problem but it's a good practice to pass `"{}"` as data instead of null (http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/) – Jupaol Jul 10 '12 at 18:52
  • 2
    I cannot find anything wrong with your code, I just created a similar service with the same configuration and it works correctly, the only difference is the `bindingConfiguration="secure"`, I guess you did not post that binding for security reasons, well if you could post it would be useful. Have you tried to remove that binding configuration and see if you can access your service?? – Jupaol Jul 10 '12 at 19:32
  • I tried removing it and I still get the same error. I added the binding configurations to my question above. I think it may be something with my method. I did some more testing and it seems to have a problem with the object HIPUser being returned, which is an object created using the ADO.NET Entity Data Model. – jocey Jul 10 '12 at 19:44
  • @jocey - Do you have Fiddler available to intercept the response? What happens if GetCurrentUser() returns a string (for testing purposes) instead of a HIPUser object? – Channs Jul 10 '12 at 19:52
  • I haven't used Fiddler before. Will go look into that. It I change the method to a string it does get called, so it definitely must be something wrong with the HIPUser object I am returning. – jocey Jul 10 '12 at 20:51
  • Might be this is your situation: http://stackoverflow.com/questions/1271490/jquery-why-does-post-does-a-get-instead-of-a-post – paramosh Jul 11 '12 at 09:45

1 Answers1

1

I was able to figure this out. There was an issue with the return object being an unknown type, so it wasn't able to be serialized into a JSON object. Wasn't sure how to serialize the object to get it to work with my method, so I ended up just changing the method to return a string constructing my own custom JSON string, and using the eval() function to turn it into a JSON object.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
jocey
  • 252
  • 4
  • 14