使用FormData和Ajax发送HTML

时间:2016-12-24 18:22:23

标签: javascript ajax asp.net-mvc post asp.net-mvc-5

我想发送textarea(TinyMCE)的HTML,但输入参数为空。

我错过了什么使它工作?任何线索?

$('#btnCreateContent').click(function () {

                // Checking whether FormData is available in browser
                if (window.FormData !== undefined) {

                    var fileUpload = $("#FileUpload1").get(0);
                    var files = fileUpload.files;

                    // Create FormData object
                    var fileData = new FormData();

                    // Looping over all files and add it to FormData object
                    for (var i = 0; i < files.length; i++) {
                        fileData.append(files[i].name, files[i]);
                    }

                    // Adding one more key to FormData object
                    fileData.append('englishTitle', $("#inputEnglishTitle").val());
                    fileData.append('englishContent', $("#editorEnglishContentCreate").val());

                    fileData.append('spanishTitle', $("#inputSpanishTitle").val());
                    fileData.append('spanishContent', $("#editorSpanishContentCreate").val());


                    $.ajax({
                        url: '@Url.Action("CreateContent", "Content")',
                        type: "POST",
                        contentType: false, // Not to set any content header
                        processData: false, // Not to process data
                        data: fileData,
                        success: function (data) {

                        },
                        error: function (err) {
                            alert(err.statusText);
                        }
                    });
                } else {
                    alert("FormData is not supported.");
                }
            });
            //

C#

[HttpPost]
public JsonResult CreateContent(string englishTitle, string englishContent, string spanishTitle, string spanishContent)
{

// englishContent is empty :(

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

var englishContent = tinyMCE.get('editorEnglishContentCreate').getContent({ format: 'raw' });
console.log(englishContent);
fileData.append('englishContent', englishContent);

C#代码也需要像

一样进行调整
[ValidateInput(false)]
[HttpPost]
public JsonResult CreateContent(string englishTitle, string englishContent, string spanishTitle, string spanishContent)
{