Skip to main content
 首页 » 编程设计

c#之无法通过 AJAX 发送大文本

2024年01月13日35findumars

所以我使用 TEXTAREA 标签发送评论文本(最多 1500 个字符)但它不起作用。

当我只使用几个词发送时,它工作正常。

任何线索,伙计们?

错误

Failed to load resource: the server responded with a status of 400 (Bad Request)  

JS

var commentText = $("#commentTextArea").val(); 
        if (commentText.length > 0) { 
             var urlGetComments = window.location.protocol + '//' + window.location.host + "/Gadget/PostComment/" + userId  + "/" + commentText; 
             $.ajax({ 
                 type: "POST", 
                 url: urlGetComments, 
                 data: "", 
                 contentType: "application/json; charset=utf-8", 
                 dataType: "json", 
                 success: function (dataComments) { 
                     if (dataComments.html != null) { 
                         $('#divCommentsData').html(dataComments.html); 
                     } 
                 } 
             });  
        } 

C#

[AcceptVerbs("POST")] 
[HttpPost] 
[ValidateInput(false)] 
public JsonResult PostComment(string userId, string commentText) 
{ 
    try 
    { 

请您参考如下方法:

您正在使用 URL 传递信息,许多服务器对 URL 中的字符有限制。由于您的操作方法接受 POST 请求,您可以将数据包含在“数据”参数中,而不是将其附加到 URL。

$.ajax({ 
    type: "POST", 
    url: urlGetComments, 
    data: { 'commentText': commentText }, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (dataComments) { 
        if (dataComments.html != null) { 
            $('#divCommentsData').html(dataComments.html); 
        } 
    } 
});