不允许使用JSONP WCF方法

时间:2013-04-12 00:16:38

标签: c# javascript jquery wcf jsonp

我的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我得到了

  

“NetworkError:405方法不允许 -   http://192.168.1.100/WebContent.svc/GetUrlContent?callback=jQuery183043170534494375234_1365725164391&[%22http://cnn.com%22,%22http://bbc.com%22]&_=1365725173310"

EDIT 这是我的新错误消息

 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()

1 个答案:

答案 0 :(得分:2)

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);