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

yipeiwu_com6年前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 stripslashes和addslashes的区别

我们在向mysql写入数据时,比如: 复制代码 代码如下:mysql_query("update table set `title`='kuhanzhu's blog'");那就会出错。...

PHP为表单获取的URL 地址预设 http 字符串函数代码

复制代码 代码如下: if (!preg_match("/^(http|ftp):/", $_POST['url'])) { $_POST['url'] = 'http://'.$_PO...

工厂模式在Zend Framework中应用介绍

工厂模式在Zend Framework中应用介绍

首先我们先引用些概念: 工厂模式:专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有其同的父类。工厂模式属于类的创建模式,通常根据自变量的不同返回不同类的实例。 工厂模式的实质...

PHP中常用的转义函数

1. addslashesaddslashes对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如...

加强版phplib的DB类

复制代码 代码如下:<?php /*************************************************************************...