wcf REST Services and JQuery Ajax Post: Method not allowed

有些话、适合烂在心里 提交于 2019-12-03 03:51:21

I see multiple problems in your code:

405 means method not allowed - it can mean that you are posting data to wrong resource. Are you sure that your address is correct? How do you expose the service? Is it .svc file or ServiceRoute? If it is .svc file the address will be UserService.svc/UserService/ValidateUser

  • UserService.svc because this is entry point for your service (if you are using ServiceRoute you can redefine this
  • UserService because you are defining this relative address in endpoint configuration
  • ValidateUser because that is default entry point for your operation

Now your JSON request is completely bad and your method signature as well. Method signature in your service contract must expect single JSON object = it must be single data contract like:

[DataContract]
public class UserData
{
    [DataMember]
    public string UserName { get; set; }

    [DataMember]
    public string Password { get; set; }
}

and operation signature will be:

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
ServiceObject ValidateUser(UserData userData);

There is no wrapper element in JSON request and because of that you must use Bare. Also it is not needed to set response format because you will set it on endpoint level (btw. you must also set request format if you don't).

Once you defined data contract for your request you must correctly define ajax request itself:

$.ajax({
  type: 'POST',
  url: "UserService.svc/UserService/ValidateUser",
  data: '{"UserName":"newuser","Password":"pwd"}',
  contentType: "application/json; charset=utf-8", 
  success: function (msg) {
    alert(msg);
  },

  error: function (xhr, ajaxOptions, thrownError) {
    alert('error');
  }

});

JSON object is as string! and all its members as well!

For last modify your configuration to:

<system.serviceModel>
  <services>
    <service name="UserService.UserService">
      <endpoint address="UserService" kind="webHttpEndpoint" contract="UserService.IUserService" />
    </service>
  </services>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  <standardEndpoints>
    <webHttpEndpoint>
      <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
    </webHttpEndpoint>
  </standardEndpoints>
</system.serviceModel>

If you want to use standardEndpoint you must use kind in endpoint definition and you don't need to specify behavior (it is part of standard endpoint). Also you are not using cross domain calls so you don't need to enable them and you don't need default format because it is resolved automatically.

I believe Ivan is on the right track here!

You are calling your service from javascript in a browser, right?

Does the html page with that javascript reside in the same domain as the wcf service?

If they are not in the same domain, then I would say that it is a cross-site-scripting issue. I believe GET is allowed cross-sites, but POST are not. http://en.wikipedia.org/wiki/JSONP would be a solution, if it's supported server-side (by WCF)

You tested on one domain I suppose the author try to make call from different domain. It could be impossible due to cross domain calls.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!