[英]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);
}
是因為我試圖從跨域訪問?
這可能是由RFC 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.