2016-12-24 79 views
-1

我想發送textarea(TinyMCE)的HTML,但輸入參數爲空。使用FormData和Ajax發送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

使用TinyMCE的API來獲取內容或至少要更新元素之前,你試圖得到它的價值 – charlietfl

+0

你是什麼意思的「englishContent是空的」?當'$ .ajax()'被調用時,'#editorEnglishContentCreate'元素的值是否不是空字符串? – guest271314

+0

@charlietfl看來你是對的請讓我知道如何去做 –

回答

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) 
{ 
相關問題