为什么ajax请求中的JSON或为什么不

nic*_*ick 5 php ajax jquery json

我想知道何时通过ajax以json数组的形式从php页面提取数据是合理的.假设我有这样的代码:

$.ajax({
    type: "get",
    url: "assets/update_cart_user_fstore.php",
    data: up,
    cache: false,
    success: function(r){
            $(#item).html(r);
    },
});
Run Code Online (Sandbox Code Playgroud)

在PHP页面我回应一个变量

$hello = "I am code!";
echo $hello;
Run Code Online (Sandbox Code Playgroud)

并使用JSON

$.ajax({
    type: "get",
    url: "assets/update_cart_user_fstore.php",
    data: up,
    cache: false,
    success: function(r){
        var obj = jQuery.parseJSON(r);
        $('#item').html(obj.code);
    },
});
Run Code Online (Sandbox Code Playgroud)

在PHP中,我正在回应JSON数组

$hello = "I am code!";
$response = array();
$response['code'] = $hello;
echo json_encode($response);
Run Code Online (Sandbox Code Playgroud)

现在我知道,如果回显多于1个变量JSON是合适的......但是这里有必要吗?我正在使用JSON吗?

请解释..