Home > Backend Development > PHP Tutorial > php 怎么接收前端传来的json数据

php 怎么接收前端传来的json数据

WBOY
Release: 2016-06-13 12:46:18
Original
3282 people have browsed it

php 如何接收前端传来的json数据
前端用JQ 生成一个有字段名和值格式的键值对 的JSON 格式的字串 转码后 提交给后台的PHP 处理
代码如下

<br />
	json_data+="\"emp_id\":\""+emp_id+"\",\"action_type\":\""+action_type+"\"})"<br />
	//	$("#emp_no_id").val(json_data);<br />
		var json_data1=eval(json_data);<br />
	//	var json_data1=json_data;<br />
<br />
//	$.each(json_data1,function(item,value){<br />
//	 alert(item+value);<br />
//	});<br />
	$.ajax({<br />
            type: "post",//使用post方法访问后台<br />
            dataType: "text",//返回json格式的数据<br />
	    url: "updata_emp.php", //要访问的后台地址<br />
	    data:json_data1,<br />
	    ontentType:'utf8',<br />
	    async:false,<br />
            success: function(msg){//msg为返回的数据,在这里做数据绑定<br />
		    var arr=msg;<br />
		    alert(msg);<br />
	    }<br />
       });<br />
Copy after login


后台使用PHP 接受
<br />
require '.\require\db_set.php';<br />
if (!empty($GLOBALS['HTTP_RAW_POST_DATA']))<br />
{<br />
    $command =  isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");<br />
    $j =json_decode( $command,true);//true,转化成数组<br />
}<br />
<br />
echo $j ;<br />
<br />
Copy after login

现在的情况是 接收到的内容 是一个字串, 无法用json_decode 转成数组



应该如何处理

PHP JSON 前端
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template