[英]Aurelia PHP Post request empty $_POST
I'm having trouble making POST requests to a server, my data is not send back to me. 我在向服务器发出POST请求时遇到问题,我的数据没有发送回给我。
I have a very simple PHP script: 我有一个非常简单的PHP脚本:
Server script 服务器脚本
<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
echo json_encode("{user-id:" . $_POST["user_id"] . "}");
?>
I want to make a POST request to this script, and get some JSON data as response. 我想对此脚本发出POST请求,并获取一些JSON数据作为响应。
Working request 工作要求
If I make a POST request from HTML, this works perfectly: 如果我从HTML发出POST请求,则效果很好:
<form method="post" action="http://url/post.php">
<input type="hidden" name="user_id" value="123" />
<button>Go to user 123</button>
</form>
This also works: 这也适用:
$.post( "url/post.php", this.data)
.done(function( data ) {
console.log( data );
});
Response 响应
"{user-id:123}"
Not working script (desire): Aurelia Fetch Client (JS2016) 无法正常运行的脚本(所需):Aurelia Fetch Client(JS2016)
submit(){
let comment = { user_id: "234" };
this.http.fetch('post.php', {
method: 'post',
body: json(comment)
})
.then(response => response.json())
.then(data => console.log(data));
}
Fetch client configuration 获取客户端配置
config
.useStandardConfiguration()
.withBaseUrl('url')
.withDefaults({
mode: 'cors',
headers: {
'Accept': 'application/json'
}
});
});
Response 响应
{user-id:}
php alternative PHP替代
I've tried to see what's inside the array in PHP, but I got the same result using implode
: 我试图查看PHP数组中的内容,但是使用
implode
得到了相同的结果:
echo json_encode("{user-id:" . (string)implode(" ",$_POST) . "}");
It looks like Aurelia does not post the data the right way. 看起来Aurelia没有正确发布数据。 Do I make a mistake here, or is it some configuration setting I'm not aware of?
我在这里出错还是我不知道的某些配置设置?
Instead of "_POST["user_I'd"]" do this 代替“ _POST [“ user_I'd”]“
$input = file_get_contents('php://input');
$input = json_decode($input);
//access user_id like this:
$input->{'user_id'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.