gpt4 book ai didi

javascript - axios POST 上的 PHP Post 数组为空

转载 作者:可可西里 更新时间:2023-11-01 00:08:13 25 4
gpt4 key购买 nike

我正在试用 Vue 2.0 和 axios,我遇到了一个小问题。当我尝试使用 axios 向我的 post.php 文件发送发布请求时,$_POST 数组始终为空。

后置函数:

doPost: function() {
console.log("post in progress")
axios.post('api/post.php', {
title: 'foo',
body: 'bar',
userId: 1
})
.then(response => {
console.log(response)
console.log(response.data)
this.filter = response.data
})
.catch(e => {
this.errors.push(e)
})
}

post.php

<?php
header('Content-Type: application/x-www-form-urlencoded');


echo json_encode($_POST);
?>

请求已完成,状态为 200,但返回一个空对象“[]”

注意:当我将 post 端点更改为 jsonplaceholder 工具时,它工作正常。

最佳答案

我觉得你可以试试这个,应该是数据类型的问题。

var data = new FormData();
data.append('title', 'foo');
data.append('body', 'bar');

axios.post('api/post.php', data)
.then(response => {
console.log(response)
console.log(response.data)
this.filter = response.data
})
.catch(e => {
this.errors.push(e)
});

关于javascript - axios POST 上的 PHP Post 数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44799589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com