Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法使用AngularJS post方法获取正确的数据_Php_Jquery_Angularjs_Codeigniter_Http Post - Fatal编程技术网

Php 无法使用AngularJS post方法获取正确的数据

Php 无法使用AngularJS post方法获取正确的数据,php,jquery,angularjs,codeigniter,http-post,Php,Jquery,Angularjs,Codeigniter,Http Post,这是我第一次和AngulerJS在一起。我正在尝试将数据发布到服务器 AngularJS代码 var app = angular.module("myApp", []); var BASE_URL = "http://localhost/project/api/Data/"; var GET_DATA = BASE_URL+"get_data"; console.log(GET_DATA); app.controller('myCtrl', function($scope, $http) {

这是我第一次和AngulerJS在一起。我正在尝试
数据发布到服务器

AngularJS代码

 var app = angular.module("myApp", []);
var BASE_URL = "http://localhost/project/api/Data/";

var GET_DATA = BASE_URL+"get_data";
console.log(GET_DATA);
app.controller('myCtrl', function($scope, $http) {
var inputs = { "user_id": "3"};
var config = {
              headers : {
                  'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
              }
          }
$http.post(GET_DATA , inputs, config)
          .success(function (response, status, headers, config) {
              $scope.content = response.error;
              $scope.statuscode = response.status;
              $scope.statustext = response.statusInfo;
              console.log(response);    
          })
          .error(function (data, status, header, config) {
              $scope.ResponseDetails = "Data: " + data +
                  "<hr />status: " + status +
                  "<hr />headers: " + header +
                  "<hr />config: " + config;
          });

});
这是错误的结果,我期待类似的结果

Array
(
    user_id => 3
)

注意:我在服务器端使用CodeIgniter框架。

您可以在
json
中发送帖子数据:

$http.post(GET_DATA , {"user_id": "3"})
      .success(function (response, status, headers, config) {
          $scope.content = response.error;
          $scope.statuscode = response.status;
          $scope.statustext = response.statusInfo;
          console.log(response);    
       })
      .error(function (data, status, header, config) {
          $scope.ResponseDetails = "Data: " + data +
             "<hr />status: " + status +
             "<hr />headers: " + header +
             "<hr />config: " + config;
       });
});
当请求内容类型为application/x-www-form-urlencoded`时,您必须以
urlencoded
格式在帖子正文中对数据进行编码。应该是这样的:

$postdata = json_decode(file_get_contents('php://input'));
print_r($postdata);
var inputs = 'student_id=3';

虽然写老问题的答案不是正确的做法,但我想把这个答案贴出来,以便其他人在需要的时候能得到帮助

首先,了解发生这种事情的原因。原因是,AngularJS不会自动序列化传递给POST请求的数据(参数)。因此,我们必须使用作为AngularJS服务提供的
$httpParamSerializerJQLike
序列化数据。发布数据的默认内容类型也是application/json。因此,我们需要将post请求的内容类型头重写为application/x-www-form-urlencoded,以便通过$\u post访问发布的值

现在在angular中还提供了
$httpParamSerializer
服务,但区别在于
$httpParamSerializerJQLike
是一个对象,也包含另一个对象,因此如果使用前者,然后内部对象将不会被序列化,即所有嵌套对象将不会使用
$httpParamSerializer
进行序列化,但
$httpParamSerializerJQLike
的情况并非如此

您可以在此处检查差异:

现在,我们可以在Angular JS中使用
$httpParamSerializerJQLike
服务,而不是在
php
中对数据进行编码和解码,方法如下:

$http({
        url: myUrl,   //post your url here
        method: 'POST',
        data: $httpParamSerializerJQLike({ 'user_id': '3'}),
       headers: {
       'Content-Type': 'application/x-www-form-urlencoded'
       }
      });
在codeigniter中,您可以通过正常语法获得发布的数据,即
$this->input->post('user_id')

有关更多信息…您可以参考上面提到的链接


希望它能帮助您……

var inputs=“user_id=3”@MayankVadiya如果我想发送多个key:value怎么办?和文件,而不是您可以使用的
&
$http({
        url: myUrl,   //post your url here
        method: 'POST',
        data: $httpParamSerializerJQLike({ 'user_id': '3'}),
       headers: {
       'Content-Type': 'application/x-www-form-urlencoded'
       }
      });