不允许使用 JSONP WCF 方法

Posted

技术标签:

【中文标题】不允许使用 JSONP WCF 方法【英文标题】:JSONP WCF Method Not Allowed 【发布时间】:2013-04-04 08:55:32 【问题描述】:

我在下面定义了我的 WCF 联系人

   [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "GetUrlContent"

         )]
    List<string> GetUrlContent(List<string> urls);

我有

<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />

我的 JS 是这样的

    var Url = "http://192.168.1.100/WebContent.svc/GetUrlContent?callback=?";
var postdata= [];
postdata.push("http://cnn.com");
postdata.push("http://bbc.com");

    $.getJSON(Url , JSON.stringify(postdata), function (msg) 
        for (i in msg) 

            console.log(msg[i]);
        
    );

Err Msg i Get is

"NetworkError: 405 Method Not Allowed - http://192.168.1.100/WebContent.svc/GetUrlContent?callback=jQuery183043170534494375234_1365725164391&[%22http://cnn.com%22,%22http://bbc.com%22]&_=1365725173310"

编辑 这是我的新错误信息

 Exception type: InvalidOperationException 
    Exception message: Operation 'GetUrlContent' in contract 'IFetchWebContent' uses GET, but also has body parameter 'urls'. GET operations cannot have a body. Either make the parameter 'urls' a UriTemplate parameter, or switch from WebGetAttribute to WebInvokeAttribute.
   at System.ServiceModel.Description.WebHttpBehavior.ValidateGETHasNoBody(OperationDescription operation, String method)
   at System.ServiceModel.Description.WebHttpBehavior.<>c__DisplayClass10.<>c__DisplayClass13.<GetRequestDispatchFormatter>b__d()

【问题讨论】:

前段时间我也遇到过这个问题。我认为问题在于 WCF 中的内置 JSONP 支持不允许您执行 POST,只能执行 GET 修复了它,但不是我遇到了不同的错误:) 请更新您的问题以包含新的错误消息。 @p.s.w.g 我用新的错误更新了它 【参考方案1】:

WCF 的内置 JSONP 支持仅限于 GET 请求;您将无法使用POST

尝试将您的方法更改为GET

[OperationContract]
[WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "GetUrlContent")]
List<string> GetUrlContent(List<string> urls);

【讨论】:

以上是关于不允许使用 JSONP WCF 方法的主要内容,如果未能解决你的问题,请参考以下文章

jQuery .ajax() 405(不允许的方法)/跨域

WCF 服务返回“方法不允许”

NetworkError:WCF 中不允许使用 405 方法

使用 jquery 查询 wcf rest 服务(不允许 405 方法)

WCF 请求失败,HTTP 状态为 405:不允许方法

未找到 WCF 端点/不允许使用 Windows 服务的方法