angularJs用于PHP的$ _POST中的POST数据

时间:2015-08-01 05:34:43

标签: javascript php angularjs post

我正在通过我的AngularJS应用程序对php脚本进行发布请求。我通过在线查找答案得到了它的工作,但我想在$_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错误。

我有没有办法在$_POST变量中获取值,因为我从未使用此file_get_contents("php://input");

如何检查是否设置了值 在$_POST我会做这样的事情:

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

如何在此file_get_contents("php://input");

中执行相同的测试

1 个答案:

答案 0 :(得分:1)

使用变换形式请求方法添加角度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){
        }
    });