使用thinkphp框架開(kāi)發(fā),不知道如何返回JSON字符串,ajax無(wú)法調(diào)用 有三種方式可以返回
元寶網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,元寶網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為元寶上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的元寶做網(wǎng)站的公司定做!
1、若是數(shù)據(jù)庫(kù)實(shí)體,可以使用toJson()方法
$user = User::get(1); echo $user-toJson();
2、若非數(shù)據(jù)庫(kù)實(shí)體,可以使用json view方法,thinkphp除了實(shí)現(xiàn)response對(duì)象,還實(shí)現(xiàn)了jsonResponse若非數(shù)
$data = [??? 'result'='success',??? 'msg'='操作成功!' ]; //class Json extends Response
return json($data);
3、若非數(shù)據(jù)庫(kù)實(shí)體,可以使用json_encode()方法:
$data = [??? 'result'='success',??? 'msg'='操作成功!' ];
return json_encode($data);
thinkphp5 ajax如何返回json
1、php 輸出JSON格式,使用json_encode函數(shù)即可
2、示例:
?php
header('Content-type: text/json');
$fruits = array (
"fruits" = array("a" = "orange", "b" = "banana", "c" = "apple"),
"numbers" = array(1, 2, 3, 4, 5, 6),
"holes" = array("first", 5 = "second", "third")
);
echo json_encode($fruits);
?
示例得到結(jié)果:
{"fruits":{"a":"orange
?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
$arr=json_decode($Tdata,true);
//var_dump($arr);
echo stripcslashes ($arr[0][A_Name]);
?
上面的方法可以打印輸出,json_decode($Tdata)輸出的結(jié)果是一個(gè)對(duì)應(yīng),而json_decode($Tdata,true)輸出的是一個(gè)數(shù)組