为什么php服务不获取变量?

为什么php服务不获取变量?,php,ajax,angularjs,Php,Ajax,Angularjs,从UI我打电话: $http.post('services/loadCategory.php', { 'id' :'1', 'type' :'string' }).then(function(response) { debugger; ... }, function(response) { ... }); 在PHP服务上,我无法从body POST请求中获取变量: include ("bd.php"); header("

从UI我打电话:

$http.post('services/loadCategory.php', {
    'id'    :'1',
    'type'  :'string'
}).then(function(response) {
    debugger;
    ...
}, function(response) {             
    ...
});
在PHP服务上,我无法从body POST请求中获取变量:

include ("bd.php");
header("Content-type: text/html; charset=windows-1251");
// ----- ----- ----- ----- -----
if (isset($_POST['type'])) {
    $type = $_POST['type'];
}
if (isset($_POST['id'])) {
    $id = $_POST['id'];
}   
//
exit(json_encode(
    array('type' => iconv('windows-1251', 'UTF-8', $_POST['type']), 
          'id' => iconv('windows-1251', 'UTF-8', $_POST['id'])
)));

来自服务的请求:{id:'',类型:'}如何修复?

将JSON发布到PHP时,$\u POST变量为空。要在PHP中获取原始JSON,请使用以下命令:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $data = json_decode(file_get_contents('php://input'), true);
}
然后,您可以使用
$data['id']
$data['type']


print\r($data)检查传入的$data

在对这个问题进行快速搜索之后,PHP似乎很难对AngularJS发送的帖子正文进行反序列化。AngularJS发送的所有信息都是JSON编码的(
application/JSON
),而大多数其他JavaScript变体则以
application/x-www-form-urlencoded
的形式发送内容

要解决此问题,您应该将请求的内容类型设置为
application/x-www-form-urlencoded
,或者您可以尝试下面一个类似问题的解决方案

基于此,以下代码(由Felipe Miosso提供)似乎可以解决此问题:

  // Your app's root module...
  angular.module('MyModule', [], function($httpProvider) {
  // Use x-www-form-urlencoded Content-Type
  $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

  /**
   * The workhorse; converts an object to x-www-form-urlencoded serialization.
   * @param {Object} obj
   * @return {String}
   */ 
  var param = function(obj) {
    var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

    for(name in obj) {
      value = obj[name];

      if(value instanceof Array) {
        for(i=0; i<value.length; ++i) {
          subValue = value[i];
          fullSubName = name + '[' + i + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += param(innerObj) + '&';
        }
      }
      else if(value instanceof Object) {
        for(subName in value) {
          subValue = value[subName];
          fullSubName = name + '[' + subName + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += param(innerObj) + '&';
        }
      }
      else if(value !== undefined && value !== null)
        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
    }

    return query.length ? query.substr(0, query.length - 1) : query;
  };

  // Override $http service's default transformRequest
  $httpProvider.defaults.transformRequest = [function(data) {
    return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  }];
});

$\u POST
有任何内容吗?(使用
print\r()
进行检查)。那么
$\u请求
?工作)呢。位我不明白-为什么简单的$\u POST不起作用?@Darienfowkes这是因为大多数前端设置,如jQuery,使用内容类型x-www-form-urlencoded和常见的?key1=foo&key2=bar序列化来传输POST数据。AngularJS使用内容类型:application/json和{“key1”:“foo”,“key2”:“bar”}。虽然这同样好,但PHP不知道如何在本地处理这一问题。
$params = json_decode(file_get_contents('php://input'),true);