json

本文详细介绍了如何使用PHP结合jQuery处理JSON数据及AJAX返回的JSON形式数据,包括如何将字符串转换为JSON对象以及如何正确解析JSON数据进行业务逻辑处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

echo json_encode返回的json编码的字符串类型

post( url [, data ] [, success ] [, dataType ] )
    • url
      Type:  String
      A string containing the URL to which the request is sent.
    • data
      Type:  PlainObject or  String
      A plain object or string that is sent to the server with the request.
    • success
      Type:  FunctionObject data,  String textStatus,  jqXHR jqXHR )
      A callback function that is executed if the request succeeds. Required if  dataType is provided, but can be  null in that case.
    • dataType
      Type:  String
      The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).

jquery post会自动判断类型 echo json_encode($xxx) 输出的是字符串
所以需要 

parseJSON( json )

去解析
Description: Takes a well-formed JSON string and returns the resulting JavaScript value.
parsejson是解析json格式的字符串

 
至于 tpp的ajaxreturn则直接返回的是json形式的数据而不是json一样的字符串
所以不需要parseJSON去解析
post知道它是json就会按照json格式处理



ajaxreturn的前端
$.post('/newinfo/index.php/Index/login',$(this).serialize(),function(data){
if(data.flag==0){
window.location.href='/newinfo/index.php/MainPage/index';
}else if(data.flag==1){
alert('E-mail address does not exist!');
}else{
alert('Email address and password do not match!');
}
},"json");

json_encode的前端

$.post('php/user_mgr.php',$(this).serialize(),function(data){
if($.parseJSON(data).flag==0){
window.location.href='ir_main.php';
}else if($.parseJSON(data).flag==1){
alert('E-mail address does not exist!');
}else{
alert('Email address and password do not match!');
}
});



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值