Axios发布的参数不是由$ _POST读取的 [英] Axios posting params not read by $_POST
问题描述
所以我有这个代码:
axios({
method: 'post',
url,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: {
json,
type,
}
})
最初我有正常的 axios。发布
但我改为这个因为我认为它可能是一个标题问题。但是我仍然在我的 $ _ REQUEST
中没有检测到任何内容,也没有检测到 $ _ POST
。但是,它在 file_get_contents(php:// input)
中接收数据。
Originally I had the normal axios.post
but I changed to this because I thought it might have been a header problem. However I am still detecting nothing in my $_REQUEST
nor $_POST
. However, it is receiving data in file_get_contents("php://input")
.
知道出了什么问题吗?
编辑
好吧我想我知道什么是错的。它将它作为json对象发布,因此只能在php://输入中读取。如何在axios中将其更改为普通字符串?
Okay I think I know what's wrong. It's posting it as a json object so it can only be read in the php://input. How do I change it to a normal string in axios?
推荐答案
来自文档(我没有在引用的材料中保留链接):
From the documentation (I haven't preserved links in the quoted material):
使用application / x-www-form-urlencoded格式
默认情况下,axios将JavaScript对象序列化为JSON。要以application / x-www-form-urlencoded格式发送数据
,您可以使用
以下选项之一。
Using application/x-www-form-urlencoded format
By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.
在浏览器中,您可以按如下方式使用URLSearchParams API:
In a browser, you can use the URLSearchParams API as follows:
var params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);
请注意,所有浏览器都不支持URLSearchParams,但
是可用的polyfill(确保填充全局
环境。)
Note that URLSearchParams is not supported by all browsers, but there is a polyfill available (make sure to polyfill the global environment).
或者,您可以使用qs库对数据进行编码:
Alternatively, you can encode data using the qs library:
var qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 }));
这篇关于Axios发布的参数不是由$ _POST读取的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!