Skip to main content
 首页 » 编程设计

javascript之如何使用 ajax (MVC) 传递 html 的内容

2024年01月24日29mate10pro

我需要从特定表单中获取 HTML 并将其保存在我项目中的 HTML 文件中。

所以我有生成器 ajax 函数,它将这个 HTML 发送到服务器,然后我保存它。

如果我发送字符串 "Hello word",我在向服务器发送 HTML 时遇到问题,所有工作,但与 "<p>Hello</p>"它不工作。

使用 ajax 发送 HTML 内容的正确方法是什么?

我知道 [ValidateInput(false)]但它不起作用

这是我的 Controller :

 [HttpGet] 
 [ValidateInput(false)] 
 public void UpdateHtml(string html) 
     { 
        try 
         { 
             string path1 = @"D:\test\my.html"; 
             System.IO.File.WriteAllText(path1, html); 
 
                } 
                catch (Exception ex) 
                { 
                } 
 
            } 

这是我的ajax:

function btn_Upload_Click() { 
 
    var html = tinyMCE.activeEditor.getContent();//my html 
  //var html = "Hello"//Like this all working 
    var token = $('input[name="__RequestVerificationToken"]').val();//checked:OK 
    var funcUrl = $("#btn_saveHtml").data("urlaction");//checked :OK 
    $.ajax({ 
        url: funcUrl, 
        type: 'GET', 
        dataType : "text/xml", 
        data: { 
            html:html, 
            __RequestVerificationToken: token 
        }, 
 
        success: function (data) 
        { 
            alert("Good"); 
        }, 
        error: function (xhr, status, error) { 
 
            alert(xhr.responseText); 
            alert(xhr.status); 
            alert(error); 
        } 
    }); 
 
} 

更新 2:我要发送的 html:

"<p style=\"text-align: center; font-size: 15px;\"><img title=\"TinyMCE Logo\" src=\"//www.tinymce.com/images/glyph-tinymce@2x.png\" alt=\"TinyMCE Logo\" width=\"110\" height=\"97\" /></p>\n<h1 style=\"text-align: center;\">Welcome to the TinyMCE editor demo!</h1>\n<h1><img style=\"float: right; padding: 0 0 10px 10px;\" title=\"Tiny Husky\" src=\"//www.tinymce.com/docs/images/tiny-husky.jpg\" alt=\"Tiny Husky\" width=\"304\" height=\"320\" /></h1>\n<h2>Image Tools Plugin feature<br />Click on the image to get started</h2>\n<p>Please try out the features provided in this image tools example.</p>\n<p>Note that any <strong>MoxieManager</strong> file and image management functionality in this example is part of our commercial offering &ndash; the demo is to show the integration.</p>\n<h2>Got questions or need help?</h2>\n<ul>\n<li>Our <a href=\"https://www.tinymce.com/docs/\">documentation</a> is a great resource for learning how to configure TinyMCE.</li>\n<li>Have a specific question? Visit the <a href=\"http://community.tinymce.com/forum/\">Community Forum</a>.</li>\n<li>We also offer enterprise grade support as part of <a href=\"www.tinymce.com/pricing\">TinyMCE Enterprise</a>.</li>\n</ul>\n<h2>A simple table to play with</h2>\n<table>\n<thead>\n<tr>\n<th>Product</th>\n<th>Cost</th>\n<th>Really?</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>TinyMCE</td>\n<td>Free</td>\n<td>YES!</td>\n</tr>\n<tr>\n<td>Plupload</td>\n<td>Free</td>\n<td>YES!</td>\n</tr>\n</tbody>\n</table>\n<h2>Found a bug?</h2>\n<p>If you think you have found a bug please create an issue on the <a href=\"https://github.com/tinymce/tinymce/issues\">GitHub repo</a> to report it to the developers.</p>\n<h2>Finally ...</h2>\n<p>Don't forget to check out our other product <a href=\"http://www.plupload.com\" target=\"_blank\">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.</p>\n<p>Thanks for supporting TinyMCE! We hope it helps you and your users create great content.<br />All the best from the TinyMCE team.</p>"

我的 ajax 返回空错误,所以我无法为您提供更多信息。

请您参考如下方法:

这是ajax请求

$.ajax({ 
        url: '@Url.Action("Test")', 
        type: 'POST', 
        datatype: 'text/xml', 
        data: { "html": "<p style=\"text-align: center; font-size: 15px;\"><img title=\"TinyMCE Logo\" src=\"//www.tinymce.com/images/glyph-tinymce@2x.png\" alt=\"TinyMCE Logo\" width=\"110\" height=\"97\" /></p>\n<h1 style=\"text-align: center;\">Welcome to the TinyMCE editor demo!</h1>\n<h1><img style=\"float: right; padding: 0 0 10px 10px;\" title=\"Tiny Husky\" src=\"//www.tinymce.com/docs/images/tiny-husky.jpg\" alt=\"Tiny Husky\" width=\"304\" height=\"320\" /></h1>\n<h2>Image Tools Plugin feature<br />Click on the image to get started</h2>\n<p>Please try out the features provided in this image tools example.</p>\n<p>Note that any <strong>MoxieManager</strong> file and image management functionality in this example is part of our commercial offering &ndash; the demo is to show the integration.</p>\n<h2>Got questions or need help?</h2>\n<ul>\n<li>Our <a href=\"https://www.tinymce.com/docs/\">documentation</a> is a great resource for learning how to configure TinyMCE.</li>\n<li>Have a specific question? Visit the <a href=\"http://community.tinymce.com/forum/\">Community Forum</a>.</li>\n<li>We also offer enterprise grade support as part of <a href=\"www.tinymce.com/pricing\">TinyMCE Enterprise</a>.</li>\n</ul>\n<h2>A simple table to play with</h2>\n<table>\n<thead>\n<tr>\n<th>Product</th>\n<th>Cost</th>\n<th>Really?</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>TinyMCE</td>\n<td>Free</td>\n<td>YES!</td>\n</tr>\n<tr>\n<td>Plupload</td>\n<td>Free</td>\n<td>YES!</td>\n</tr>\n</tbody>\n</table>\n<h2>Found a bug?</h2>\n<p>If you think you have found a bug please create an issue on the <a href=\"https://github.com/tinymce/tinymce/issues\">GitHub repo</a> to report it to the developers.</p>\n<h2>Finally ...</h2>\n<p>Don't forget to check out our other product <a href=\"http://www.plupload.com\" target=\"_blank\">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.</p>\n<p>Thanks for supporting TinyMCE! We hope it helps you and your users create great content.<br />All the best from the TinyMCE team.</p>" }, 
        error: function () { 
            console.log("error"); 
        }, 
        success: function () { 
 
        } 
    }) 

和服务器代码

    [HttpPost] 
    [ValidateInput(false)] 
    public void Test(string html) 
    { 
 
    } 

当然我没有任何 token 。我可以在 Debug模式下从服务器捕获 html。