小编Mol*_*lod的帖子

未捕获的SyntaxError意外的数字JSON.parse

尝试通过ajax请求获取json数据,但始终收到此错误:

Uncaught SyntaxError Unexpected Number
Run Code Online (Sandbox Code Playgroud)

这是我的js代码:

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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

这是我的PHP代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

调用error_log以查看我尝试接收的json数据:

{"test":"test"}
Run Code Online (Sandbox Code Playgroud)

我之前在其他项目上使用过ajxa + json,这一切都很好。我不知道如何解决它:(

php ajax wordpress jquery json

1
推荐指数
1
解决办法
5095
查看次数

标签 统计

ajax ×1

jquery ×1

json ×1

php ×1

wordpress ×1