百分求助:php解析Jquery ajax post的Json数据 本帖最后由 teacher1998 于 2013-11-27 22:37:32 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当 contentType 为 application/x-www-form-urlencoded 时(默认值)才能用 $_POST 得到传入的数据而你的却是 application/json;charset=utf-8这并不是 php 所能识别的类型声明,不能替你解析。所以你只能用 php://input 取得,并用 parse_str 自行解析而形如 a=1&b=2&c=3 这样的数据,分明就不是 json 格式。即便 php 能识别 application/json 类型,也不能解析你这样的数据 LZ,你把 2个文件的contentType都去掉就可以了。 to : 玛特斯维尔定律, 你这个就变成plain text了,不是json。to: xuzuning 版主,那php怎么解析json格式的数据呢? 我这分明就是json数据啊。 用json_decode 转化为php可读取的值 我试过了,json_decode不行啊, 汗,这个怎么会不可以!你把你的json数据贴出个例子来 你的 $str = {"id":5, "cat":4, "price":3, "num": 2}; 是 js 对象s = '{"id":"5", "cat":"4", "price":"3", "num":"2"}';才是可被 json_decode 解析的 json 数据$.ajax 会将 js 的对象转换成形如 id=5&cat=4&price=3&num=2 这样的字符串,称之为序列化(serialize)而形如 id=5&cat=4&price=3&num=2 这样的字符串,在web开发中称之为 uri参数串php 会自动将其转换到 $_POST 或 $_GET 数组中,当然前提是你传递了正确的类型声明 To: xuzuning 版主,多谢你的提点,差不多弄好了。多谢。后台我这样弄: header('Content-type: application/json'); $data = json_decode(file_get_contents("php://input"), true); $id = $data["id"]; $cat = $data["cat"]; $price = $data["price"]; $num = $data["num"]; echo $id." ".$cat." ".$price." ".$num;前台这样$("#submitBtn").click(function(){ //$str = {"id":5, "cat":4, "price":3, "num": 2}; $str = '{"id":"5", "cat":"4", "price":"3", "num":"2"}'; $.ajax({ type: 'POST', url: 'update.php', data: $str, dataType:'json', contentType:"application/json;charset=utf-8", success: function(data) { $("#result").html(JSON.parse(data)); }, error: function(XMLHttpRequest, textStatus, errorThrown) { $("#result").html("failed content: " + $str['id']+" " + $str['cat'] + " " + $str['price'] + " " + $str['num'] + "<br>failed reason: " + textStatus+" " + errorThrown + "<br/>" + XMLHttpRequest.responseText); //alert("error " + XMLHttpRequest.responseText); alert(JSON.parse(data)); } });});输出:failed content: undefined undefined undefined undefinedfailed reason: parsererror SyntaxError: Unexpected number5 4 3 2为什么还会报错呢?版主帮看看?这两天才弄php,小白之处见谅,谢谢。 错误是哪里出的?请给全错误信息看上去不像是 php 报的错你 js 部分 有 dataType:'json'表示要求将返回内容当做 json 解释而你在 php 中 echo $id." ".$cat." ".$price." ".$num;显然不是输出一个 json 数据 版主在前台的登录页面跟普通会员一样吗 怎么把链接显示的文字传递给其他页面 JavaScript + HTML PHP 聊天功能原理 (是类似网页上的客服系统__非聊天室) php与sql_server的分页 一个棘手的MYSQL中文乱码问题,先声明我是看过置顶精华贴才问的,因为还是没有搞定 急!如何避免刷新导致FORM提交的数据重复处理?? 怎样用php制作网页上的铃声试听 一个php的问题,请帮忙。 PHP从MSSQL中读取备注型字段自动截断问题 win7,apache,php5.2.6,报错行数和实际行数差了很多(不是少个括号那种错,是缺个参数那种) IIS 6 伪静态规则问题
才能用 $_POST 得到传入的数据
而你的却是 application/json;charset=utf-8
这并不是 php 所能识别的类型声明,不能替你解析。所以你只能用 php://input 取得,并用 parse_str 自行解析
而形如 a=1&b=2&c=3 这样的数据,分明就不是 json 格式。即便 php 能识别 application/json 类型,也不能解析你这样的数据
s = '{"id":"5", "cat":"4", "price":"3", "num":"2"}';
才是可被 json_decode 解析的 json 数据$.ajax 会将 js 的对象转换成形如 id=5&cat=4&price=3&num=2 这样的字符串,称之为序列化(serialize)而形如 id=5&cat=4&price=3&num=2 这样的字符串,在web开发中称之为 uri参数串
php 会自动将其转换到 $_POST 或 $_GET 数组中,当然前提是你传递了正确的类型声明
$data = json_decode(file_get_contents("php://input"), true); $id = $data["id"];
$cat = $data["cat"];
$price = $data["price"];
$num = $data["num"];
echo $id." ".$cat." ".$price." ".$num;前台这样$("#submitBtn").click(function(){ //$str = {"id":5, "cat":4, "price":3, "num": 2};
$str = '{"id":"5", "cat":"4", "price":"3", "num":"2"}';
$.ajax({
type: 'POST',
url: 'update.php',
data: $str,
dataType:'json',
contentType:"application/json;charset=utf-8",
success: function(data) {
$("#result").html(JSON.parse(data));
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#result").html("failed content: " + $str['id']+" " + $str['cat'] + " " + $str['price'] + " " + $str['num'] + "<br>failed reason: " + textStatus+" " + errorThrown + "<br/>" + XMLHttpRequest.responseText);
//alert("error " + XMLHttpRequest.responseText);
alert(JSON.parse(data));
}
});
});
输出:
failed content: undefined undefined undefined undefined
failed reason: parsererror SyntaxError: Unexpected number
5 4 3 2为什么还会报错呢?版主帮看看?这两天才弄php,小白之处见谅,谢谢。
表示要求将返回内容当做 json 解释
而你在 php 中 echo $id." ".$cat." ".$price." ".$num;
显然不是输出一个 json 数据