javascript - 预检响应在 ionic + CI 后端中具有无效的 HTTP 状态代码 405

标签 javascript angularjs codeigniter ionic-framework codeigniter-3

我正在尝试使用 codeigniter 后端在 ionic 中开发第一个应用程序,但面临预检响应在 ionic 中存在无效的 HTTP 状态代码 405 问题 + CI 后端问题帮助我解决这个问题。

我的controller.js:

.controller('SingUpCtrl',function($scope,$http){
$scope.signup_cancel = function (){
$state.go('app.login');
};

$scope.signUp=function(){
$scope.data = {};
var postData = {
  "user_first_name":$scope.data.user_first_name,
  "user_last_name" :$scope.data.user_last_name,
  "user_gender" :$scope.data.user_gender,
  "user_dob" :$scope.data.user_dob,
  "user_email" :$scope.data.user_email,
  "user_password" :$scope.data.user_password
};

var link = 'http://maghnusideas.com/ionic/index.php/api/places/user';
var header={
  "cache-control": "no-cache",
      "postman-token": "6cfae124-631b-9367-89c7-04c0a12ab489"
}
$http.post(link, postData,header).then(function (res){
  console.log('success');
  var json_obj = JSON.stringify(res.data);
  json_obj = JSON.parse(json_obj);
  $ionicPopup.alert({
    template: json_obj.message
  });

});
};

我在 codeigniter 中的 webapi 代码:

public function user_post()
{
    $this->load->library('form_validation');
    $this->load->model('Admin_model');

    $this->form_validation->set_rules('first_name', 'First Name', 'required');
    $this->form_validation->set_rules('last_name', 'Last Name', 'required');
    $this->form_validation->set_rules('gender', 'Gender', 'required');
    $this->form_validation->set_rules('dob', 'Date of Birth', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if($this->form_validation->run())
    {
        $first_name=$this->input->post('first_name');
        $last_name=$this->input->post('last_name');
        $gender=$this->input->post('gender');
        $dob=$this->input->post('dob');
        $email=$this->input->post('email');
        $password=$this->input->post('password');
    }
    else{
        $this->set_response('Failure to add', REST_Controller::HTTP_BAD_REQUEST);
    }

    $result=$this->Admin_model->insert('tbl_user',array('user_first_name'=>$first_name,'user_last_name'=>$last_name,'user_gender'=>$gender,'user_dob'=>$dob,'user_email'=>$email,'user_password'=>$password));
    if($result)
    {
        $this->set_response('User Registered Successfully...!', REST_Controller::HTTP_OK);


    }else
    {
        $this->set_response('Inserted Data not Proper', REST_Controller::HTTP_BAD_REQUEST);

    }
}

最佳答案

此错误与 CORS 有关 要解决此问题,您可以使用 ionic 代理功能 - 请参阅http://ionicinaction.com/blog/how-to-fix-cors-issues-revisited/ 或这里 http://blog.ionic.io/handling-cors-issues-in-ionic/

关于javascript - 预检响应在 ionic + CI 后端中具有无效的 HTTP 状态代码 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39286606/

相关文章:

javascript - 如何在 Angular 捕获 block 中传递参数

php - 如何在codeigniter中使用事件类制作if else菜单

php - 如何检查两个文件是否存在

php - 我想创建一个图像 slider ,自动滑动文件夹中的所有相似图像

javascript - NodeJS子进程偶尔停止

angularjs - 使用 angularjs ui-router 捕获任何 url

php - 将值发布到 MySQL 数据库

javascript - 快速 jQuery 问题 : Stopping event propagation?

javascript - 删除和编辑 knockout observableArray 中的项目

javascript - 如何重新排序 angularjs ngTable 分页?