如何读取AngularJS发送在PHP POST请求数据 [英] How to read POST Request data in PHP sent from AngularJS
问题描述
我试图让从我的角度JS POST请求,如下
I am trying to make a post request from my angular js, as below
var postData={
firstName:'VenuGopal',
lastName:'Kakkula'
};
postData=JSON.stringify(postData);
$http({method:'POST',url:'http://localhost/blog/posttest.php?insert=true',data:postData,headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.success(function (data,status,headers,config) {
alert('Success' + data);
})
.error(function (data, status,headers,config) {
// uh oh
alert('error' + status + data);
});
我不知道如何在我的PHP REST Web服务阅读这篇文章的数据。
I am not sure how to read this POST DATA in my PHP REST webservice.
<?php
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers:Content-Type');
header("Content-Type:application/json");
if(!empty($_GET['insert']))
{
$result=json_decode($_POST['firstName']);
deliver_response(200,"Got the Post data","GotData $result");
}
function deliver_response($status,$statusmsg,$data)
{
header("HTTP/1.1 $status $statusmsg");
$response['status']=$status;
$response['status_message']=$statusmsg;
$response['data']=$data;
$json_response=json_encode($response);
echo $json_response;
}
?>
我试过下面的选项
json_decode($_POST['firstName'])
json_decode($_POST['data'])
json_decode($_POST['[postData'])
他们没有返回的数据,它总是返回错误未定义指数:在F中姓:\\ XAMPP \\ htdocs中\\ 10号线博客\\ posttest.php
None of them returned the data, Its always returning the error Undefined index: firstName in F:\xampp\htdocs\Blog\posttest.php on line 10
任何人可以帮助我,我怎么可以读PHP文件发送POST请求的数据。
Can anybody help me how I can read the POST request data sent in php file.
推荐答案
最有可能是因为您要提交未自动的填充到每标准表单数据的$ _POST变量JSON数据。
Most likely it is because you are submitting JSON data which is not automagically populated into the $_POST variable per standard form data.
解决方法是手动解析投入使用的变量。
The work around is to manually parse the input into a variable for use.
例如:
<?php
$args = json_decode(file_get_contents("php://input"));
echo $args->firstName;
?>
这篇关于如何读取AngularJS发送在PHP POST请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!