skip to Main Content

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


  1. "{"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}

    Login or Signup to reply.
  2. Set the content type to json:

    jQuery.ajax({
      url: couponspining_ajaxurl,
      type: "POST",
      contentType: "application/json",
      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));
    });
    
    Login or Signup to reply.
  3. Simply change the parse json line from

    this.submit_form_done(jQuery.parseJSON(json));
    

    to

    this.submit_form_done(json);
    

    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().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search