2013-03-21 46 views
0

下面的函数在输入字符串(#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); 
    } 

是不是因为我想从跨域访问?

+1

您需要显示您的服务器逻辑以及它的工作时间和时间的一些示例 – 2013-03-21 16:30:35

+0

您是否正确地转义了要在url中使用的字符串? – 2013-03-21 16:31:24

+0

@ w.brian您的意思是转义? – Khan 2013-03-21 16:34:36

回答

1

长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)] 
+0

它抛出405方法不允许错误 – Khan 2013-03-21 17:03:46

+0

@ Khan你必须改变atttribute [WebGet]到[WebInvoke(Method =“POST”)] – Corneliu 2013-03-21 17:06:40

+0

我改变为[WebPost(ResponseFormat = WebMessageFormat.Json)]但它不识别WebPost – Khan 2013-03-21 17:09:54

1

这可能是由R​​FC GET请求中的限制造成的。看看this question

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

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

它抛出405方法不允许错误 – Khan 2013-03-21 16:54:36

+0

@Khan:也许这是由跨域请求造成的。一个常见的解决方案是使用jsonp。有关详细信息,请参阅[此答案](http://stackoverflow.com/a/3506306/473475):) 但是,这可能也是一个服务器端问题。我不是一个asp开发人员,我真的不知道如何调试,很抱歉。 – 2013-03-21 21:32:25

0

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