I want to use jQuery.ajax for sending new mark(about article) to database and changind number of marks on page. But I receive bad AJAX response from server.
{"likes":"40","dislikes":"29"}{"success":true,"message":null,"messages":null,"data":[]}
and error:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 31 of the JSON data
It seems there must not be the second ajax response. But I'm not understand where it have taken.
That is my php code from Joomla plugin.
$marks = plgContentLikesHelper::getMarks($articleID);
$data=array();
$data['likes'] = $marks->likes;
$data['dislikes'] = $marks->dislikes;
echo json_encode($data);
That is my jQuery code from Joomla plugin.
jQuery(document).ready(function(){
jQuery('div.plg-likes > a ').click(function (e) {
var id = jQuery(this).parent('div.plg-likes').attr('id');
var opinion = jQuery(this).attr('id');
jQuery.ajax({
type: "POST",
dataType: "json",
url: "index.php?option=com_ajax&group=content&plugin=likes&format=json",
data: { articleId: id, articleOpinion: opinion },
success: function(data){ /* troubles are here */
alert(data["likes"]);
}
})
return false;
})
})
I have took the advise and used JResponseJson
$db->setQuery($query);
$db->execute();
$marks = plgContentLikesHelper::getMarks($articleID);
$data=array();
$data['likes'] = $marks->likes;
$data['dislikes'] = $marks->dislikes;
//echo json_encode($data);
echo new JResponseJson($data);
The response has changed but mistake still be present.
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 84 of the JSON data
{"success":true,"message":null,"messages":null,"data":{"likes":"6","dislikes":"0"}}{"success":true,"message":null,"messages":null,"data":[]}
Can you help me find where I'm wrong?