Javascript angularJs在php的$u POST中发布数据

Javascript angularJs在php的$u POST中发布数据,javascript,php,angularjs,post,Javascript,Php,Angularjs,Post,我正在对AngularJS应用程序中的php脚本进行post请求。我通过在线查找答案使其正常工作,但我想在$\u POST['jwt']变量中获取php脚本中的数据 以下是我的AngularJS代码: var dataReq = { method: 'POST', url: 'http://localhost/PHP/dataSender.php', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data

我正在对AngularJS应用程序中的php脚本进行post请求。我通过在线查找答案使其正常工作,但我想在
$\u POST['jwt']
变量中获取php脚本中的数据

以下是我的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的错误

是否有一种方法可以获取
$\u POST
变量中的值,因为我从未使用过此
文件\u get\u内容(“php://input");

如何检查值是否已设置 在
$\u POST
中,我会这样做:

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

如何在此
文件中执行相同的测试php://input");

使用transform请求方法添加一个angular 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 );

     }
     );
添加表单保存控制器,并在控制器文件中使用转换表单请求和头

 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){
        }
    });