【问题标题】:angularJs POST data in $_POST for phpangularJs POST 数据在 $_POST for php
【发布时间】:2015-10-23 20:50:21
【问题描述】:

我正在向我的 AngularJS 应用程序的 php 脚本发出发布请求。我已经通过在线查找答案让它工作了,但我想在我的 php 脚本中的 $_POST['jwt'] 变量中获取数据。

这是我的 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");做同样的测试

【问题讨论】:

    标签: javascript php angularjs post


    【解决方案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){
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      相关资源
      最近更新 更多