Unexpected end of JSON input while sending tinyMCE data using Ajax

一曲冷凌霜 提交于 2019-12-12 05:25:23

问题


I am using tinyMCE for some user data and to save that data i am using Ajax to send data.

When I tried to save the data using ajax i get this two errors:

parsererror

SyntaxError: Unexpected end of JSON input

SyntaxError: Unexpected token < in JSON at position 0 (after Some changes i get this another error)

Here is my code.This is the HTML code:

<textarea id="cover_letter" name="cover_letter" class="cover_letter_tinyMCE"></textarea> 
<button type="button" class="btn" id="save_cover_letter">Save Cove Letter</button>

This is the Ajax:

$(document).on("click", "#save_cover_letter", function (e) {     
e.preventDefault();

var cover_letter_text = tinyMCE.editors[$('.cover_letter_tinyMCE').attr('id')].getContent(); 
console.log(cover_letter_text);

    $.ajax({                    
        url: save_url,
        async: true,
        type: 'POST',           
        data: {'cover_letter':cover_letter_text},    
        dataType: 'json',                               
        success : function(response) {      
          alert(response);
          console.log(response);                  
        },               
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert(jqXHR);
            alert(textStatus);
            alert(errorThrown);    
        }

    });

 });

回答1:


Use Form HTML

<form id="cover_letter">
    <textarea  name="cover_letter" class="cover_letter_tinyMCE"></textarea> 
    <button type="submit" class="btn" id="save_cover_letter">Save Cove Letter</button>
</form>

AND jQuery

$(document).on("submit", "#cover_letter", function (e)
{     
    e.preventDefault(); 

    $.ajax({                    
        url: save_url,
        async: true,
        type: 'POST',           
        data: $(this).serialize(),
        dataType: 'json',                               
        success : function(response) {      
          alert(response);
          console.log(response);                  
        },               
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert(jqXHR);
            alert(textStatus);
            alert(errorThrown);    
        }
    });
 });



回答2:


I had a similar issue, I recommend trying following:

1) See whether your "save_url" script returns any unclosed < through echo.

2) Also check whether your console.log(cover_letter_text) outputs the desired content from the text area.



来源:https://stackoverflow.com/questions/45213204/unexpected-end-of-json-input-while-sending-tinymce-data-using-ajax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!