jQuery.Deferred异常错误

时间:2018-01-31 10:56:36

标签: jquery json ajax

在此代码中,我收到此错误。我试图在网上查找,但我似乎没有得到任何答案。

jQuery.Deferred exception: Unexpected token < in JSON at position 0 
SyntaxError: Unexpected token < in JSON at position 0
at Function.parse [as parseJSON] (<anonymous>)
at Object.<anonymous> (http://localhost/test/comment_insert.js:44:24)
at l (http://localhost/test/jquery-3.3.1.min.js:2:29375)
at c (http://localhost/test/jquery-3.3.1.min.js:2:29677)

我做错了什么?

.then(function(data) {
    comment_insert(jQuery.parseJSON(data));
    console.log("ResponseText: " + data);
}); 

编辑2:我提供了完整的代码。我仍然不明白如何解决这个问题。我还是新编码。也许通过提供完整的代码,你们可能会看到我想要做的事情。

&#13;
&#13;
$( document ).ready( function(){
	
	//this will fire when the page is fully loaded
	
	$( '#comment-post-btn' ).click( function(){
		comment_post_btn_click();
	
	});
	
	
});



function comment_post_btn_click(){
	
	//records the text within the textarea	
		var _comment = $( '#comment-post-text').val();
		var _userId = $( '#userId').val();
		var _userName = $( '#userName').val();
		
		
		if( _comment.length > 0 && _userId != null){
			
			//proceed with our ajax callback
	$('.comment-insert-container').css('border' , '1px solid #e1e1e1');
						
						
	$.post("comment_insert.php" ,
	{
						
	task : "comment_insert",
	userId : _userId,
	comment : _comment	
	} 
	)
	.catch(function()
	{
	console.log("Error: " );
	})
					
	.then(function(data)
	{
	comment_insert(jQuery.parseJSON(data));
	console.log("ResponseText: " + data);
		});	
						
						
 console.log(_comment + " Username: " + _userName + " User Id:" + _userId);
		}
		else{
			// displays message when textarea is empty
			$('.comment-insert-container').css('border' , '1px solid #ff0000');
			console.log("The text area was empty");
		}
		
		
		//this line of code clears the text area when button is clicked
		$( '#comment-post-text').val("");
	
	
}



function comment_insert(data){
	
	
	var t = '';
	t += '<li class="comment-holder" id="_'+data.comment_id+'">';
    t += '<div class="user-img">';
	t += '<img src="'+data.profile_img+'" class="user-img-pic"/>';
	t += '</div>';
	t += '<div class="comment-body">';
	t += '<h3 class="username-field">'+data.userName+'</h3>';
	t += '<div class="comment-text">n'+data.comment+'</div>';
	t += '</div>';
	t += '</div>';	
	t += '<div class="comment-buttons-holder">';
	t += '<ul>';
	t += '<li class="delete-btn">X</li>';
	t += '</ul>';
	t += '</div>';
	t += '</li>';
	
	
	$('.comments-holder-ul').prepend( t );
	
	
	
}
&#13;
&#13;
&#13;

0 个答案:

没有答案