【问题标题】:angular: http post not sending data to php file角度:http post没有将数据发送到php文件
【发布时间】:2017-05-06 17:40:57
【问题描述】:

我遇到了 angular ajax 请求的奇怪问题。 基本上我通过角度http请求将两个数据发送到php文件。 这是代码。

var allData={'city_id': city, 'uid': uid}

 $http({
      method  : 'POST',
      url     : 'update_exec.php',
      data : allData,
     // headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers  
     headers: {'Content-type': 'application/json'}
  })

这是 php 文件。

  $uid = $_POST['uid'];
    $city = $_POST['city_id'];
echo $uid;
echo $city;

var_dump($_POST);

var_dump 的输出为空白。

当我签入萤火虫时,我在 XHR 中得到以下详细信息。 状态 -200 方法 - 选项 但参数中没有数据。

如果我将标题更改为 { 'Content-Type': 'application/x-www-form-urlencoded' } 这个。

然后我得到 Status - 200 和 Method - POST 我也可以在参数中看到数据。

但是对于这两种情况,我们都不会在 php 文件中输入数据。

【问题讨论】:

  • angular & api 是在同一个域还是不同的域?
  • 它们在同一个域中。对于获取请求,它工作正常。
  • 你可以试试打印var_dump( file_get_contents('php://input'));
  • var_dump(file_get_contents('php://input')); 没有输出。
  • NULL 正在显示在var_dump 上?

标签: php angularjs


【解决方案1】:

如果您需要发送对象,则无需手动添加任何标头。试试这个

 var allData={'city_id': city, 'uid': uid}

 $http({
      method  : 'POST',
      url     : 'update_exec.php',
      data : allData
  }).then(function mySucces(response) {
        //SUCCESS CALLBACK
    }, function myError(response) {
        //ERROR CALLBACK
    });

在 PHP 中,您可以通过$_POSTfile_get_contents('php://input') 获取数据 第一个为您提供 POST 数据;后一个提供原始输入。如果需要使用 json 标头,则需要将 allData 通过JSON.stringify(allData) 转换为 JSON 字符串然后发送。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多