jQuery以json格式从php中获取结果

时间:2013-01-08 05:49:56

标签: php javascript jquery

我有一个$.post如何从php中获取结果?

$.post("sql/customer_save.php",{ what: "edit",customer_no: $customer_no});

php回复

echo json_encode(array("customer_id"=>$customer_id,"customer_no"=>$customer_no));

我应该把$ .post放在什么地方才能得到结果?

1 个答案:

答案 0 :(得分:4)

$.post("sql/customer_save.php",{ what: "edit",customer_no: $customer_no},function(resp)
{
   //resp is your response
   //You can try
    console.log(resp);
    console.log(resp["customer_id"]); //get the value of customer_id
    console.log(resp["customer_no"]); //get the value of customer_no
},"json");

您也可以使用

 $.post("sql/customer_save.php",{ what: "edit",customer_no: $customer_no},function(resp)
    {
       //resp is your response
       //You can try
        var data = $.parseJSON(resp);
        console.log(data);
        console.log(data["customer_id"]); //get the value of customer_id
        console.log(data["customer_no"]); //get the value of customer_no
    });