将html内容发布到控制器方法会删除部分内容

posting html content to controller method removes part of content

本文关键字:方法 删除部 控制器 html      更新时间:2023-09-26

这是js函数:

    var onContentChange = function () {
        var content =   
          $("#blogpost-content").data("kendoEditor").value($("#value").val());
        console.log(content);
        $http.post("/Map/SaveBlogPostContent?destinationId=" + 
           $("#currentDestinationId").val() + 
           "&blogPostId=" + $("#currentBlogPost").val() + "&content=" + content)
                .then(onSaveBlogPostContent, onError);
    }

此方法是在更改文本区域时触发的。当它到达console.log时,它会写出当前文本区域中的正确文本,但当它将数据发布到我的控制器方法时,它只接收一部分内容。为什么会这样?

很可能您的文本区域中有"非法字符",请尝试转义内容变量,并将其更改为:

var onContentChange = function () {
    var content = $("#blogpost-content").data("kendoEditor").value($("#value").val());
    console.log(content);
    $http.post("/Map/SaveBlogPostContent?destinationId=" + encodeURIComponent($("#currentDestinationId").val()) + "&blogPostId=" + encodeURIComponent($("#currentBlogPost").val()) + "&content=" + encodeURIComponent(content))
            .then(onSaveBlogPostContent, onError);
}

encodeURIComponent()是其中的重要部分。

由于这是一个get而不是post,在get请求中可以发送的数据长度有限制,这将取决于浏览器。你还需要确保你没有任何需要转义的字符。例如,url中使用的特许状,如?或者&或/。你可以通过使用来逃避这些。

var contentToPass = encodeURIComponent(content);

请参阅此处encodeURIComponent

试试这个:

var onContentChange = function () {
    var content =   
    $("#blogpost-content").data("kendoEditor").value($("#value").val());
    console.log(content);
    $http.post("/Map/SaveBlogPostContent", {
       destinationId: $("#currentDestinationId").val(),
       blogPostId: $("#currentBlogPost").val(),
       content: content
    }).then(onSaveBlogPostContent, onError);
}