c# - 不允许 JSONP WCF 方法

标签 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]);
        }
    });

我收到的错误消息是

"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 支持仅限于 GET 请求;您将无法执行POST

尝试将方法更改为 GET:

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

关于c# - 不允许 JSONP WCF 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15961044/

相关文章:

javascript - 调用多个元素的分页不起作用,仅调用最后一个元素

javascript - jquery 将内容加载到 div 和 magicline

php - Laravel 5 和 AJAX 响应

c# - ASP.NET如何将ASPX文件解析为可执行代码?

c# - 如何在 C# .net 中管理图书馆管理系统中的学生和图书发行关系?

javascript - 使用 Firefox 发送的 POST 请求中的字符编码问题

javascript - 在javascript中按字母顺序排序

javascript - Jquery 子选择器没有出现

c# - 无法关闭 MySQL 数据库连接

c# - 如何在我的表单停止响应的情况下填充数据 GridView ?