【问题标题】:Angularjs $http POST request empty arrayAngularjs $http POST 请求空数组
【发布时间】:2023-04-05 02:49:01
【问题描述】:

以下$http request 成功执行,但另一端的PHP 脚本在应该接收'test' 和'testval' 时接收到一个空的$_POST 数组。有什么想法吗?

$http({
    url: 'backend.php',
    method: "POST",
    data: {'test': 'testval'},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data, status, headers, config) {
    console.log(data);

    }).error(function (data, status, headers, config) {});

【问题讨论】:

    标签: javascript php angularjs


    【解决方案1】:

    如果你只想发送简单的数据,试试这个:

    $http({
        url: 'backend.php',
        method: "POST",
        data: 'test=' + testval,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data, status, headers, config) {
            console.log(data);
    
        }).error(function (data, status, headers, config) {});
    

    而php部分应该是这样的:

    <?php
        $data = $_POST['test'];
        $echo $data;
    ?>
    

    它对我有用。

    【讨论】:

    【解决方案2】:

    这是 AngularJS 的常见问题。

    第一步是更改 POST 请求的默认 content-type 标头:

    $http.defaults.headers.post["Content-Type"] = 
        "application/x-www-form-urlencoded; charset=UTF-8;";
    

    然后,使用 XHR 请求拦截器,有必要正确序列化有效负载对象:

    $httpProvider.interceptors.push(['$q', function($q) {
        return {
            request: function(config) {
                if (config.data && typeof config.data === 'object') {
                    // Check https://gist.github.com/brunoscopelliti/7492579 
                    // for a possible way to implement the serialize function.
                    config.data = serialize(config.data);
                }
                return config || $q.when(config);
            }
        };
    }]);
    

    这样,有效载荷数据将再次在 $_POST 数组中可用。

    有关XHR interceptor的更多信息。

    另一种可能,就是保留默认的content-type头,然后服务器端解析payload:

    if(stripos($_SERVER["CONTENT_TYPE"], "application/json") === 0) {
        $_POST = json_decode(file_get_contents("php://input"), true);
    }
    

    【讨论】:

      【解决方案3】:

      更简化的方式:

      myApp.config(function($httpProvider) {
          $httpProvider.defaults.transformRequest = function(data) {        
              if (data === undefined) { return data; } 
              return $.param(data);
          };
          $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; 
      });
      

      【讨论】:

      • 你应该在诅咒 PHP
      【解决方案4】:

      删除以下行和前面的逗号:

      headers: {'Content-Type': 'application/x-www-form-urlencoded'}
      

      然后数据会出现在$_POST中。如果您要上传文件,则只需要该行,在这种情况下,您必须解码正文以获取数据变量。

      【讨论】:

      • 感谢您的回复。不幸的是,我删除了这条线,但没有任何运气。我的 PHP 脚本中的 print_r($_POST) 命令仍然返回一个空数组。
      【解决方案5】:

      我在这里找到了我的解决方案http://www.peterbe.com/plog/what-stumped-me-about-angularjs。 “AJAX 不像 jQuery”部分中有一段代码,它解决了我的问题。

      【讨论】:

        猜你喜欢
        • 2017-08-24
        • 2013-08-16
        • 2016-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-29
        • 1970-01-01
        相关资源
        最近更新 更多