php - Uncaught SyntaxError Unexpected Number JSON.parse -
trying json data through ajax request, error :
uncaught syntaxerror unexpected number here 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 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 see json data trying receive:
{"test":"test"} i used ajxa+json before on other projects , good. have no idea how fix :(
by specifying datatype: 'json', data passed success function deserialized object, don't call json.parse on it
success: function(data){ console.log(data); }, although should give error syntaxerror: unexpected token o , not uncaught syntaxerror unexpected number. should check developer tools see returned server.
Comments
Post a Comment