angularJs POST在$ _ POST数据PHP [英] angularJs POST data in $_POST for php

查看:142
本文介绍了angularJs POST在$ _ POST数据PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个POST请求一个PHP脚本形成我AngularJS应用程序。我得到它通过上网找答案工作,但我想获得在 $ _ POST ['智威汤逊'] 可变我的PHP脚本的数据。

I am doing a post request to a php script form my AngularJS app. I have gotten it working by looking up answers online but I would like to get the data in my php script in the $_POST['jwt'] variable.

下面是我的AngularJS code:

Here is my AngularJS code:

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我有这样的:

In my PHP I have this :

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$jwt = $request->jwt;
echo $jwt;

和它的工作,但如果我改变了头在我的角度要求

And it is working but if I change the header in my angular request to

'Content-Type': 'application/json'

我得到的CORS错误。

I get the CORS error.

有没有一种方法可以让我在 $ _ POST 变量,因为我从来没有与这个的file_get_contents工作(PHP得到的值://输入);

Is there a way I can get the values in $_POST variable because I have never worked with this file_get_contents("php://input");

我如何检查的值设置与否
$ _ POST 我会做这样的事情:

How can I check for the values is set or not in $_POST I would do something like this:

if(isset($_POST["jwt"]) && !empty($_POST["jwt"]))
 {
  echo $_POST["jwt"];
  }

我怎样才能做到这的file_get_contents相同的测试(PHP://输入);

推荐答案

我具有用户注册的例子说明见下图:

I explain with user registration example see below:

要POST数据到Web服务器,您需要在您的表单值绑定到 $范围一个对象,然后提交该对象的脚本。

To post data to a web server, you will require to bind your form values to a object in the $scope, and then submit that object to the script.

关键是要提交整个对象的用户到服务器,并在角JSON会自动格式化。另外,用户没有被使用的纳克模型标记

The trick is to submit the entire object "user" to the server, and Angular will automatically format it in JSON. Also, "user" was not being used by the ng-model tags.

另外要注意的是,你可能要包括的东西应用程序时,它完成的要求做。您可以使用的方法.success(功能(数据){})和.error(...)做这个(这些都是承诺$ HTTP回报的方法)。

Another thing to note is that you will probably want to include something for the app to do when it finishes the request. You can use the methods ".success(function(data){})" and ".error(...)" to do this (these are methods on the promise $http returns).

从角HTML JSON数据:

JSON data from Angular HTML:

<form ng-controller="UserController" ng-submit="createUser()">

    <legend>Create User</legend>

    <label>Name</label> 
    <input type="text" id="name" name="name" ng-model="user.name" placeholder="User Name"> 

    <label>Email</label>
    <input type="text" id="email" name="email" ng-model="user.email" placeholder="ur email here"> 

    <label>Password</label>
    <input type="text" id="pwd" name="pwd" ng-model="user.pwd" placeholder="ur own pwd here">

    <button class="btn btn-primary">Register</button>

</form>

控制器:

function UserController($scope, $http) {
    $scope.user = {};
    $scope.createUser = function() {
        $http({
            method : 'POST',
            url : '/create',
            data : $scope.user
        })
    }

AngularJS 。员额()默认的Content-type头应用程序/ JSON。您正在重写此通过形式恩codeD数据,但是你不改变你的数据值来传递适当的查询字符串,因此PHP没有填充 $ _ POST 如您所愿。

AngularJS .post() defaults the Content-type header to application/json. You are overriding this to pass form-encoded data, however you are not changing your data value to pass an appropriate query string, so PHP is not populating $_POST as you expect.

我的建议是使用应用程序/ JSON作为标题的默认设置angularjs,阅读PHP原始输入,然后反序列化JSON。

My suggestion would be to use the default angularjs setting of application/json as header, read the raw input in PHP, and then deserialize the JSON.

PHP:

$fileData = file_get_contents("php://input");
$objFileData = json_decode($fileData);

$pwd = $objFileData -> pwd;//Get user password
$user = $objFileData -> name; //Get user name

有关详细请参考 $ HTTP 的文档。
也许这个帮助。

For more detail refer Documentation of $http. Perhaps this helps.

这篇关于angularJs POST在$ _ POST数据PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆