【问题标题】:Querying A wcf rest service with jquery (405 Method Not Allowed)使用 jquery 查询 wcf rest 服务(不允许 405 方法)
【发布时间】:2014-07-17 17:56:09
【问题描述】:

我创建了一个由 jquery 使用的 WCF 休息服务。该服务托管在我的本地计算机中。该服务似乎仅在我使用 GET 动词时才有效。如果我使用任何其他我得到 405(不允许方法)错误。我想使用 jquery 向我的服务发布一个 json 对象。

[ServiceContract]
public interface ITest
{
    [OperationContract]
    [WebInvoke(Method = "PUT", UriTemplate="Contact", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    [Description("Send contact us message.")]
    string Log(Someobject object);
}

客户代码:

$.ajax({
           type: "PUT",
            url: "http://localhost/servicename/ContactUs.svc/Contact",
            data: JSON.Stringify(jsonObject),
            contentType: "application/json; charset=utf-8",
            processData:true,
            dataType: "jsonp",
            success: function (data) {
                alert("Ping" + data);
            },
            error: function (error) {
                alert("Failed to ping server" + error);
            }
        });

网页配置

<behavior name="ContactUsBehaviour">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="ContactUsEndPointBehavior">
      <webHttp helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>

<services>
  <service name="servicename.ContactUs" behaviorConfiguration="ContactUsBehaviour">
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="JSONPBinding" contract="servicename.IContactUs" behaviorConfiguration="ContactUsEndPointBehavior"/>
  </service>
</services>

【问题讨论】:

    标签: jquery json wcf rest wcf-rest


    【解决方案1】:

    请参阅 dataType: "jsonp" 仅适用于 type:"GET"。所以如果你使用jsonp,那么你必须使用这个:

    type: "GET", // or remove it, if not available then "GET" is default set
    .......
    dataType: "jsonp",
    

    Another answer which can help you.


    在您发表评论后,我建议您这样做:

    $.ajax({
         type: "GET", // change to GET
         url: "/servicename/ContactUs.svc/Contact", // remove the http://localhost
         data: JSON.Stringify(jsonObject),
         contentType: "application/json; charset=utf-8",
         processData:true,
         dataType: "json",   // change jsonp to json
         success: function (data) {
             alert("Ping" + data);
         },
         error: function (error) {
             alert("Failed to ping server" + error);
         }
    });
    

    或者如果你仍然想使用http://localhost,那么你必须在你的服务器端启用cors

    A post about enabling CORS (Cross Origin Resource Sharing.)

    【讨论】:

    • 我的目标是将对象作为参数发送到服务器,因此我使用 type:put 或 post。如果我删除 jsonp 并使用类型 post 或 put 我得到以下信息:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'null' 是不允许访问的。'我也得到 (options {my service url} 405 method not allowed
    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 2014-03-30
    • 2016-06-13
    • 2011-01-13
    • 2017-11-18
    • 2011-03-04
    • 2017-10-10
    • 1970-01-01
    相关资源
    最近更新 更多