2013-01-08 35 views
2

我有一個$.post我怎樣才能從php返回結果?jQuery從json格式的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)); 

我應該怎樣才能得到結果回來換上$。員額?

回答

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 
    }); 
+0

Amirali:如果你想知道什麼不同......在'$。員額加入一個' 「JSON」'參數'電話。這告訴jQuery期待JSON形成的響應。 – Wireblue

+0

是否有任何方法可以分別獲取每個字段?像customer_id,customer_no? –

+0

以這種方式訪問​​它們,resp.customer_id和resp.customer_no – Floricel