php 如何接收前端传来的json数据
前端用JQ 生成一个有字段名和值格式的键值对 的JSON 格式的字串 转码后 提交给后台的PHP 处理
代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <br />
json_data+= "\"emp_id\":\"" +emp_id+ "\",\"action_type\":\"" +action_type+ "\"})" <br />
var json_data1= eval (json_data);<br />
<br />
$.ajax({<br />
type: "post" ,
dataType: "text" ,
url: "updata_emp.php" ,
data:json_data1,<br />
ontentType: 'utf8' ,<br />
async:false,<br />
success: function (msg){
var arr=msg;<br />
alert(msg);<br />
}<br />
});<br />
|
登录后复制
后台使用PHP 接受
1 2 3 4 5 6 7 8 9 10 | <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);
}<br />
<br />
echo $j ;<br />
<br />
|
登录后复制
现在的情况是 接收到的内容 是一个字串, 无法用json_decode 转成数组
应该如何处理