【发布时间】:2015-10-23 20:50:21
【问题描述】:
我正在向我的 AngularJS 应用程序的 php 脚本发出发布请求。我已经通过在线查找答案让它工作了,但我想在我的 php 脚本中的 $_POST['jwt'] 变量中获取数据。
这是我的 AngularJS 代码:
var dataReq = {
method: 'POST',
url: 'http://localhost/PHP/dataSender.php',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: { jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' }
}
$http(dataReq).success(function(response,data) {
console.log(response);
});
在我的 PHP 中有这个:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$jwt = $request->jwt;
echo $jwt;
它正在工作,但是如果我将我的角度请求中的标头更改为
'Content-Type': 'application/json'
我收到 CORS 错误。
有没有办法可以获取$_POST 变量中的值,因为我从未使用过这个file_get_contents("php://input");
如何检查是否设置了值
在$_POST 我会做这样的事情:
if(isset($_POST["jwt"]) && !empty($_POST["jwt"]))
{
echo $_POST["jwt"];
}
我怎样才能在这个file_get_contents("php://input");做同样的测试
【问题讨论】:
标签: javascript php angularjs post