Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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
在angularjs请求的情况下识别来自codeigniter的Ajax请求_Angularjs_Codeigniter - Fatal编程技术网

在angularjs请求的情况下识别来自codeigniter的Ajax请求

在angularjs请求的情况下识别来自codeigniter的Ajax请求,angularjs,codeigniter,Angularjs,Codeigniter,我正在使用angularjs向我的codeigniter控制器发送请求。我测试过不同的可能性,比如 $this->input->is\u ajax\u request()和一个定制函数 function is_ajax(){ $is_ajax = false; if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ $is_ajax = true; } return $is_ajax;

我正在使用angularjs向我的codeigniter控制器发送请求。我测试过不同的可能性,比如

$this->input->is\u ajax\u request()
和一个定制函数

function is_ajax(){
  $is_ajax = false;
  if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
    $is_ajax = true;
  }
  return $is_ajax;
}
两者都不起作用

我的ajax请求代码是

var f = {};
        $scope.f = f;  

$scope.saveForm = function () {
        $scope.Error = null;
        // Try to login
        $http.post("form", f)
                .then(function (response) {
                    alert(response);
                }, function (x) {
                    $scope.Error = 'Server Error';
                });
    }

我的控制器在codeigniter中工作正常,接收请求也正常。但是我想过滤我的Ajax请求。解决方案是什么?

$http默认情况下不包括x-requested-with标头。发送请求时,需要将其添加到$http配置中。像这样的东西应该有用

$scope.saveForm = function () {
    $scope.Error = null;
    // Try to login
    $http.post("form", f, {
        'headers': { 
            'X-Requested-With' :'XMLHttpRequest'
        }
    }).then(function (response) {
        alert(response);
    }, function (x) {
        $scope.Error = 'Server Error';
    });
}

angularjs没有发送,或者codeigniter中的路由无效?您试图解决什么具体问题?angularjs正在发送请求,codeigniter可以接收请求。但我想确定上面提到的ajax请求是HTTP_X_REQUESTED_,当您进行var_转储时,它实际显示在$_服务器中吗?是的,我已经测试过了。HTTP\u X\u请求的\u不在$\u服务器中!!