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设计模式 Strategy(策略模式)

php设计模式 Strategy(策略模式)

抽象策略(Strategy)角色:定义所有支持的算法的公共接口。通常是以一个接口或抽象来实现。Context使用这个接口来调用其ConcreteStrategy定义的算法。 具体策略(C...

php实现RSA加密类实例

本文实例讲述了php实现RSA加密类。分享给大家供大家参考。具体分析如下: 通过openssl实现的签名、验签、非对称加解密,需要配合x.509证书(如crt和pem)文件使用。 由于各...

AJAX的使用方法详解

AJAX作为异步传输,局部刷新非常方便,用处很广! 首先,对于AJAX的使用有4步: 1.创建AJAX对象 var xmlHttp = new XMLHttpRequest(); 2.建...

基于xcache的配置与使用详解

一、安装Xcache复制代码 代码如下:# wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz#...

php ob_flush,flush在ie中缓冲无效的解决方法

PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。 flush() 函数不会对服务器或客户端浏览器的缓存模式...