I have an OperationContract
method where I am trying to query and insert data into a database. I use the POST
method and call the service from javascript in the browser. The WCF service is in the same domain, so I do not need to use JSONP
. I am also modifying the data, so this should be a POST
request not a GET
request. However, I still get "Method error not allowed." Has anyone encountered this situation?
My service is called in
http://some_url.com/services/ProfileService.svc/json/CurrentUser
Oddly, it seems to be called through a GET
request when I go to this URL, although I specify POST
. However, when the page loads, it tries to execute a POST
request.
Browser response when navigating to 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 necessary, 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>
Change 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>