ajax请求 json格式和数组格式总结

php
echo json_encode($data);
$.ajax({
url:APP+"?a=total&c=collection", //请求的页面
type:"post", //请求的格式
async:false, //异步
data:{platform_id:name,table:'yield'},
dataType:'json', //异步
success:function(data){ //返回数据
legendData = data.strLegend;
termField = data.field;
month = data.month;
for(var i= 0;i<termField.length;i++){
leng[i] = {'name':termField[i].name,"type":"bar","data":termField[i].zhi};
}
},
  error:funcion(er){
    BackErr(er);
  }

});

var url = "{url('Finance/down')}";
{literal}
$.post(url,{'page':'1','profit':pft,'address':ads},function(chd){
 {/literal}
var chooseData = chd.loan;
$('#removeid div').remove();
$('#removeid li').remove();
if(chd.status == 1001){
for(var i=0;i<chooseData.length;i++){
$('.ui-finlist').append("<li class="fn-clear"><a href="{url('Finance/detail')}&id="+chooseData[i].id+'"><div class="listleft"><span><img src="{iamges_url()}'+chooseData[i].logo+'" width="100%"></span><p>'+chooseData[i].feature+'</p></div><div class="listright"> <em>剩余保额</em><p>'+chooseData[i].coverage+'<span>元</span><p/><div>最高年化收益:'+chooseData[i].rate+'</div> <div>最短投资期限:'+chooseData[i].lowest_date+'</div> </div></a></li>');
}
}
if(chd.status == 1004 && nodata) {
$('#wrapper').css('background','#fff');
$('.ui-finlist').append('<div align="center" style="margin-top:50px"><img src="http://mui1.1.amoydao.com/images/noicon.jpg" height="150"></div><div align="center" style="margin-top:20px">暂无此分类的精选平台</div>');
$('#pagemain').css('backgroundColor','#fff');
}
page = 1;
loadbool=false;
maxpage=false;
myScroll.refresh();
}, "json");//设置了获取数据的类型,所以得到的数据格式为json类型的

$.get("data.php",$("#firstName.val()"),function(data){
   $("#getResponse").html(data); }//返回的data是字符串类型
);

$.getJSON("data.php",$("#firstName.val()"),function(jsonData){
  $("#getJSONResponse").html(jsonData.id);}//无需设置,直接获取的数据类型为json,所以调用时需要使用jsonData.id方式
);
示例:
$.getJSON(bangding+"?id="+z+"&jsoncallPP=?",function(obj){ //在 jQuery 1.2 中,您可以通过使用 JSONP 形式的回调函数来加载其他网域的 JSON 数据,如 "myurl?callback=?"。jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。 注意:此行以后的代码将在这个回调函数执行前执行。
isclick = true;
layer.close(indexload);
if(obj.status==1000){
var fun2 = function(){
$('#tcShow').hide();
location.href='{$plat->platform_wxaddress}';
}
renrenalert(obj.user_info,'','','','去看看',fun2);
}else if(obj.status==1001){
window.location.href = '{$plat->platform_wxaddress}';
}else if(obj.status==1099){
var fun1 = function(){
$('#tcShow').hide();
$('#enter').attr('onclick',"enter('','')");
}
var fun2 = function(){
location.href=loginurl;
}
renrenalert(obj.user_info,'','',fun1,'取消',fun2);
}else{
if(obj.user_info){
renrenalert(obj.user_info);
}else{
renrenalert('一键操作失败');
}
}
})
php:bangding.php
public function indexAction() {
$jsoncallPP = $this->input->get_post("jsoncallPP");
if(is_empty($jsoncallPP)){header404();}
//接收平台ID
$id = (int)$this->input->get_post("id",true);
//判断平台是否存在: Zlomodel:exitsPlatform
$platform = $this->Zlomodel->exitsPlatform($id);
if($platform===false){
//ajax_return(1002,"","此平台不存在,请您返回平台列表确认后再操作");
$json_str = json_encode(array('status' => 1002, 'data' =>'', 'user_info' => '此平台不存在,请您返回平台列表确认后再操作'));
echo $jsoncallPP.'('.$json_str.')'; exit();
}

//走绑定接口
$data = array(
"flatformId" => $id,
);
$urlData['url'] = api_item('bindUser');
$urlData['method'] = 'post';
$urlData['apiurls'] = 'open_api';
$urlData['params'] = $this->Curl->tokendatacurl($data);
echo $jsoncallPP.'('.json_encode($this->Curl->apicurl($urlData)).')'; exit();
//json_return($this->Curl->apicurl($urlData));
}
 


json_encode //返回 value 值的 JSON 形式 数组-》js格式

json_decode//接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 js格式-》数组(大部分情况)
原文地址:https://www.cnblogs.com/hfdp/p/5210407.html