Trying to get json data through ajax request, but always get this error :
Uncaught SyntaxError Unexpected Number
Here is my js code :
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
$.ajax({
url: ajaxurl,
type: 'POST',
dataType: 'json',
data: { action : 'getPills' },
success: function(data){
product = JSON.parse(data);
console.log(product);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
Here is my php code :
add_action('wp_ajax_getPills', 'getPills');
add_action('wp_ajax_nopriv_getPills', 'getPills');
function getPills(){
$data = array(
"test" => 'test'
);
error_log(json_encode($data), 0);
echo json_encode($data);
}
called error_log to see what json data I trying to receive:
{"test":"test"}
I used ajxa+json before on other projects and it was all good. I have no idea how to fix it :(