For the following code getting output
$response=array();
$response['wheel_deg_end'] = (360*(ceil($wheel->wheel_spin_time/3))) + (360 - (($wheel_slice_number * 30) - 30)) + rand(-5,5);
$response['wheel_time_end'] = $wheel->wheel_spin_time * 1000;
$response['success'] = true;
$ab = json_encode($response,JSON_NUMERIC_CHECK);
echo $ab;
Output:
"{"wheel_deg_end":1743,"wheel_time_end":10000,"success":true}"
and error in Json parse:
jQuery.ajax({
url: couponspining_ajaxurl,
type: 'POST',
data: {
action: 'couponspining_wheel_run',
form_data: form_data,
preview_key: this.preview_key
},
context: this,
}).done(function(json){
this.submit_form_done(jQuery.parseJSON(json));
});
Uncaught SyntaxError: JSON.parse: unexpected character at line 3
column 1 of the JSON data
submit_form http://localhost/shopify-php-app/src/public/assets/js/couponspining1.js:210
3
Answers
"{"wheel_deg_end":1743,"wheel_time_end":10000,"success":true}"
is not a valid json string.Which should be
{"wheel_deg_end":1743,"wheel_time_end":10000,"success":true}
Set the content type to json:
Simply change the parse json line from
to
Because if the configuration of your AJAX call is having dataType: json you’ll get a JavaScript object so it’s no longer necessary to use JSON.parse().