在发布这个问题之前我环顾了很多,所以我很抱歉,如果是在另一个帖子上,这只是我在这里的第二个问题,如果我没有正确地格式化这个问题,那么道歉.
我有一个非常简单的Web服务,我创建了它需要获取post值并返回一个JSON编码的数组.一切正常,直到我被告知我需要发布带有内容类型的application/json的表单数据.从那时起,我无法从Web服务返回任何值,这肯定与我如何过滤他们的帖子值有关.
基本上在我的本地设置中,我创建了一个测试页面,它执行以下操作 -
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/'); // Set the url path we want to call
$result = curl_exec($curl);
//see the results
$json=json_decode($result,true);
curl_close($curl);
print_r($json);
Run Code Online (Sandbox Code Playgroud)
在网络服务上我有这个(我已经删除了一些功能) -
<?php
header('Content-type: application/json');
/* connect to the db */
$link = mysql_connect('localhost','root','root') or die('Cannot connect to the DB');
mysql_select_db('webservice',$link) or die('Cannot select the DB');
if(isset($_POST['action']) && $_POST['action'] == 'login') {
$statusCode …Run Code Online (Sandbox Code Playgroud) 我想向PHP脚本发送POST请求.我正在使用Axios:
axios.post('inc/vote.php', {
id: this.selected,
}) .then(function (response) {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
在PHP文件中,我尝试这样的东西从axios中检索id变量:
$id = $_POST['id'];
Run Code Online (Sandbox Code Playgroud)
但似乎没有做任何事情.
从请求中检索变量的正确方法是什么?
我正在尝试Vue 2.0和axios,我有一点问题.当我尝试使用axios向post.php文件发送post请求时,$ _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)
})
}
Run Code Online (Sandbox Code Playgroud)
post.php中
<?php
header('Content-Type: application/x-www-form-urlencoded');
echo json_encode($_POST);
?>
Run Code Online (Sandbox Code Playgroud)
请求以状态200完成,但返回空对象"[]"
注意:当我将post端点更改为jsonplaceholder工具时,它工作正常.
我正在尝试使用 axios 发送数据,但它发送了一个意外的响应。当我使用邮递员发出相同的请求时,它成功地向我的手机发送了一个通知,这是邮递员的回复:
{
"status": 200,
"success": true,
"message": "Notification sent to your phone to make payment",
"data": null
}
Run Code Online (Sandbox Code Playgroud)
但是使用 axios,通知不会发送到我的手机,这是 axios 的响应:
{
status: 200,
success: true,
message: 'Tickets Sold Out',
data: null
}
Run Code Online (Sandbox Code Playgroud)
这是我的 axios 代码:
const postData = {
totalSum: 5000,
event_id: 7,
phone_number: '1298515352'
};
let axiosConfig = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
"Access-Control-Allow-Origin": "*",
}
};
axios({
method: 'post',
url: 'url',
headers: axiosConfig,
data: postData
})
.then((res) => {
const message = res.data; …Run Code Online (Sandbox Code Playgroud)