PHP Post获取不到非表单数据的问题解决办法

yipeiwu_com5年前PHP代码库

问题描述

在使用vue-axios向后端post数据时,PHP端获取不到post的数据。

问题解决

修改php.ini配置

找到php.ini配置文件,查找enable_post_data_reading变量,修改为打开状态,注释掉句前分好

; Whether PHP will read the POST data.
; This option is enabled by default.
; Most likely, you won't want to disable this option globally. It causes $_POST
; and $_FILES to always be empty; the only way you will be able to read the
; POST data will be through the php://input stream wrapper. This can be useful
; to proxy requests or to process the POST data in a memory efficient fashion.
; http://php.net/enable-post-data-reading
enable_post_data_reading = On  //大约656行,修改此条

修改配置后,发现还是不行,继续查阅资料。

获取非表单数据

搜集资料之后,发现vue-axios向后端post的是非表单数据(Ajax不同),在获取非表单数据时需要用php://input

$raw = file_get_contents('php://input');//获取非表单数据
echo $raw;//输出结果

PS:post时前端请求头要设置为

headers: {
  "Content-type": "application/json; charset=utf-8"
}

相关文章

php实现编辑和保存文件的方法

本文实例讲述了php实现编辑和保存文件的方法。分享给大家供大家参考。具体如下: save_file.php: <?php session_start(); $han...

PHP数组函数知识汇总

本文为大家分享了PHP数组函数基础知识,供大家参考,具体内容如下 数组array是非常重要的数据类型。相对于其他的数据类型,它更像是一种结构,而这种结果构可以存储一系列数值。数组能够在单...

php上传后台无法收到数据解决方法

php无法收到数据 form表单是很常用的html标签,它能为我们提交数据到服务器,上传文件等。有时后台程序却无法接收数据,下面看看解决方法吧。 一、$_POST接收不到数据,$_GET...

PHP实现的XML操作类【XML Library】

本文实例讲述了PHP实现的XML操作类。分享给大家供大家参考,具体如下: 这是一个接口程序,需要大量分析解析XML,PHP的xml_parse_into_struct()函数不能直接生成...

php求斐波那契数的两种实现方式【递归与递推】

本文实例讲述了php求斐波那契数的两种实现方式。分享给大家供大家参考,具体如下: 斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci),又称黄...