从最近两天开始,我尝试使用$ http将我的Ionic应用程序中的数据发布到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)
});
}
})
CodeIgniter控制器代码:
<?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);
}
}
}
Codeigniter模型代码:
<?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;
}
}
?>
请帮我解决这个问题。我使用的是高级CodeIgniter版本3,提前致谢
答案 0 :(得分:2)
请使用以下方法
$http({
method : "POST",
url : "http://localhost/vas_api/index.php/api/User/index/",
data:{ name:user.name }
}).then(function(response) {
console.log(response)
});