繁体   English   中英

函数不适用于长输入参数

[英]Function doesn't work with long input parameter

当输入字符串(#txtarea)包含很少字符但在包含长字符串时不起作用时,下面的函数有效,如何使它工作?

下面是我的代码:

 $('#insertcmt').click(function () {
        $.getJSON('http://localhost:55679/RESTService.svc/InsertComment?callback=?', { commenttext: $('#txtarea').val() }, function (data) {
        });
        loadcomments();

    });

服务器端逻辑:

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public void InsertComment(string commenttext)
    {
        string sql = "INSERT statement";
        Database db = Utilities.GetDataBase();
        DbCommand cmd = db.GetSqlStringCommand(sql);
        db.ExecuteNonQuery(cmd);
    }

是因为我试图从跨域访问?

这可能是由R​​FC GET请求的限制引起的。 看看这个问题

由于您在服务器端逻辑中使用了insert语句,因此您应该使用POST请求。

 $('#insertcmt').click(function () {
    $.post('http://localhost:55679/RESTService.svc/InsertComment?callback=?', { commenttext: $('#txtarea').val() }, function (data) {
    });
    loadcomments();
});

长URL(超过2000个字符)可能无法在所有Web浏览器中使用。

使用POST方法:

$('#insertcmt').click(function () {
  $.post('http://localhost:55679/RESTService.svc/InsertComment?callback=', 
    { commenttext: $('#txtarea').val() }, 
    function (data) {

    });

  loadcomments();
});

编辑:

您必须将[WebGet]属性更改为:

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]

尝试通过POST发送内容,而不是GET,理论上没有普遍的限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM