简体   繁体   English

$ _POST中用于PHP的angularJs POST数据

[英]angularJs POST data in $_POST for php

I am doing a post request to a php script form my AngularJS app. 我正在从AngularJS应用程序向PHP脚本发布请求。 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. 我通过在网上查找答案来使它起作用,但是我想在$_POST['jwt']变量的php脚本中获取数据。

Here is my AngularJS code: 这是我的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);
});

In my PHP I have this : 在我的PHP我有这个:

$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'

I get the CORS error. 我收到CORS错误。

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变量中的值,因为我从未使用过此file_get_contents("php://input");

How can I check for the values is set or not in $_POST I would do something like this: 如何检查$_POST是否设置了值,我将执行以下操作:

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

How can I do the same test in this file_get_contents("php://input"); 如何在此file_get_contents("php://input");进行相同的测试file_get_contents("php://input");

Add a angular js factory with transform form request method. 使用转换表单请求方法添加一个有角度的js工厂。

 'use strict';
          // I provide a request-transformation method that is used to prepare the outgoing
     // request as a FORM post instead of a JSON packet.
     angularApp.factory(
     "transformRequestAsFormPost",
     function() {

     // I prepare the request data for the form post.
     function transformRequest( data, getHeaders ) {

     var headers = getHeaders();

     headers[ "Content-type" ] = "application/x-www-form-urlencoded; charset=utf-8";

     return( serializeData( data ) );

     }


     // Return the factory value.
     return( transformRequest );


     // ---
     // PRVIATE METHODS.
     // ---


     // I serialize the given Object into a key-value pair string. This
     // method expects an object and will default to the toString() method.
     // --
     // NOTE: This is an atered version of the jQuery.param() method which
     // will serialize a data collection for Form posting.
     // --
     // https://github.com/jquery/jquery/blob/master/src/serialize.js#L45
     function serializeData( data ) {

     // If this is not an object, defer to native stringification.
     if ( ! angular.isObject( data ) ) {

     return( ( data == null ) ? "" : data.toString() );

     }

     var buffer = [];

     // Serialize each key in the object.
     for ( var name in data ) {

     if ( ! data.hasOwnProperty( name ) ) {

     continue;

     }

     var value = data[ name ];

     buffer.push(
     encodeURIComponent( name ) +
     "=" +
     encodeURIComponent( ( value == null ) ? "" : value )
     );

     }

     // Serialize the buffer and clean it up for transportation.
     var source = buffer
     .join( "&" )
     .replace( /%20/g, "+" )
     ;

     return( source );

     }

     }
     );


     // -------------------------------------------------- //
     // -------------------------------------------------- //


     // I override the "expected" $sanitize service to simply allow the HTML to be
     // output for the current demo.
     // --
     // NOTE: Do not use this version in production!! This is for development only.
     angularApp.value(
     "$sanitize",
     function( html ) {

     return( html );

     }
     );

Add form save controller and use a transform form request and header in controller file. 添加表单保存控制器,并在控制器文件中使用转换表单请求和标头。

 angularApp.controller('saveFormCtrl', function($scope, $http, transformRequestAsFormPost) {   
     $http({
                                transformRequest: transformRequestAsFormPost,
                                method : 'POST',
                                url : 'save.php',
                                data: {
                                    jwt: $scope.jwt,
                                },
                                headers: {
                                 'Content-Type': 'application/x-www-form-urlencoded',
                                 }
                        }).success(function(res){
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM