2017-03-18 78 views
0

從上兩天開始,我嘗試使用$ http將我的Ionic應用程序中的數據發佈到CodeIgniter rest api。但是我的發佈數據是空的。我參考了一些鏈接。但未能解決。以下是我的代碼。將POST數據從離子框架傳遞給CodeIgniter Rest API

我的離子視圖

<div class="list"> 
     <label class="item item-input item-md-label" placeholder="Username" highlight-color="balanced" type="text"> 
       <input class="md-input" type="text" ng-model="user.name"> 
       <span class="input-label">Username</span> 
       <div class="hightlight hightlight-calm"></div> 
      </label> 

      <label class="item item-input item-md-label" placeholder="Password" highlight-color="energized" type="password"> 
       <input class="md-input" type="password" ng-model="user.password"> 
       <span class="input-label">Password</span> 
       <div class="hightlight hightlight-calm"></div> 
      </label> 

     </div> 
     <div class="padding"> 
      <button ng-click="doLogin(user)" class="button button-full button-assertive ink">Login</button> 
     </div> 

離子控制器代碼:

.controller('LoginCtrl', function($scope, $timeout, $stateParams, ionicMaterialInk, $http, baseUrl) { 
    $scope.$parent.clearFabs(); 
    $timeout(function() { 
     $scope.$parent.hideHeader(); 
    }, 0); 
    ionicMaterialInk.displayEffect(); 

    $scope.doLogin = function(user) { 

    $http({ 
     method : "POST", 
     url : "http://localhost/vas_api/index.php/api/User/index/"+user.name 
    }).then(function mySucces(response) {  
     console.log(response) 
    }); 
    }  
}) 

笨控制器代碼:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

require APPPATH . '/libraries/REST_Controller.php'; 

use Restserver\Libraries\REST_Controller; 

class User extends REST_Controller { 

    public function index_post() { 
     if(!$this->input->post('name')) { 
     $this->response(null, 400); 
     } 

     $data = array(
     'name' => $this->input->post('name') 
    ); 

     $id = $this->User_model->insertUser($this->input->post('name')); 

     if(!is_null($id)) { 
     $this->response(array('response' => $id), 200); 
     } else { 
     $this->response(array('response', 'Null values'), 400); 
     } 
    } 
} 

笨型號代碼:

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class User_model extends CI_Model { 

    function insertUser($data) { 
    $this->db->insert('user',array('name' => $data)); 
    $userId = $this->db->insert_id(); 

    return $userId; 
    } 

} 

?> 

請幫我解決這個問題。我使用的是提前笨版本3預先感謝

+1

您使用後,但沒有公佈任何數據發佈數據使用'數據:{名稱:user.name}' –

+0

$ http({method:「POST」, url:「http://localhost/vas_api/index.php/api/User/index/」,data:{name:user.name} })。then (函數mySucces(響應){console.log(響應) });我需要使用上面的格式,對嗎? –

+0

@Sathosh請看看我的答案,下面也讓我知道如果它不適合你 –

回答

2

請使用以下方法

$http({ 
     method : "POST", 
     url : "http://localhost/vas_api/index.php/api/User/index/", 
     data:{ name:user.name } 
    }).then(function(response) {  
     console.log(response) 
    }); 
+1

感謝它的作品。 –