php返回json字符串的代碼(錯誤方式):
return json_encode($res);
js代碼:
$.ajax({
url: '<?php echo site_url("Cus/get");?>',
type: 'POST',
data:{'id':id},
dataType: 'json',
success:function(data){
//console.log(data);
},
error:function(arg){
//console.log(arg);
}
});
結果:php用json_encode()返回json數據字符串,ajax請求該接口,有數據但返回<empty string>
,原來是return 有問題,換成下面的方式就行了。
php方這樣就好了:
echo json_encode($res);return;
或者
exit(json_encode($res));