Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

axios post数据 php 无法获取

JS代码:

var params = {
    username: 'admin',
    password: '123456'
}

axios.post('test.php', params).then(res => console.log(res.data))

php代码:

$str = $_POST['username'];
print_r($str);

浏览器打印出来的是图片描述


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1.传的是一个json对象,可以用file_get_contents("php://input")获取到json串,然后转化为object获取属性
2.如果想用$_POST获取,修改下代码

var params = new URLSearchParams();
params.append('username', 'admin');
params.append('password', '123456');
axios.post('test.php', params).then(res => console.log(res.data));</script>

可以看下文档 Using application/x-www-form-urlencoded format这个段落


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...