0

From last two days i'm try to post the data from my Ionic app to CodeIgniter rest api using $http. But my post data goes empty. I refere some link. But failed to seolve. Below is my code.

My ionic view

 <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>

Ionic Controller code :

.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 controller code :

<?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 model code :

<?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;
  }

}

?>

Please help me to solve this issue. I'm using advance CodeIgniter version 3 Thanks in advance

3
  • 1
    You are using post but not posting any data to post data use ,data:{name:user.name} Commented Mar 18, 2017 at 7:44
  • $http({ method : "POST", url : "localhost/vas_api/index.php/api/User/index/",data:{name:user.name} }).then(function mySucces(response) { console.log(response) }); I need to use like above format, correct? Commented Mar 18, 2017 at 7:46
  • @Sathosh please have a look at my answer below also let me know if it is not working for you Commented Mar 18, 2017 at 7:49

1 Answer 1

2

Please use following approach

$http({
      method : "POST",
      url : "http://localhost/vas_api/index.php/api/User/index/",
      data:{ name:user.name }
    }).then(function(response) {      
      console.log(response)
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.