简体   繁体   中英

Cannot get response in json format when creating webservice in codeigniter

I am creating register webservice in Codeigniter. I want to get the response in json format, if the registration is successfull then the data will be returned in json format and if the data is already present then the json response will be returned. I have confusion in how to pass value from controller to view and convert it into json response. Below is my code:

Controller:

<?php

session_start(); //we need to start session in order to access it through CI

Class User_Signup extends CI_Controller {

public function __construct() {
parent::__construct();

// Load form helper library
$this->load->helper('form');

// Load form validation library
$this->load->library('form_validation');

// Load session library
$this->load->library('session');


// Load database
$this->load->model('signup_model');
}

public function registration($fname,$lname,$email) {
$data=array('first_name' => $fname,'last_name' => $lname,'email' => $email);
$result = $this->signup_model->registration_insert($data);
if ($result == TRUE) {
$this->load->view('signup_message',$data);
} else {
$this->load->view('signup_message',$data);
}
}
}

Signup_model (Model):

<?php

Class Signup_Model extends CI_Model {

// Insert registration data in database
public function registration_insert($data) {

// Query to check whether username already exist or not
$condition = "email =" . "'" . $data['email'] . "'";
$this->load->database();
$this->db->select('*');
$this->db->from('user');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 0) {

// Query to insert data in database
$this->db->insert('user', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
return false;
}
}
}
?>

View:

<?php

/* output in necessary format */
if ($format == 'json')
{
    //header('Content-type: application/json');

    echo str_replace('\/', '/', json_encode($posts));
} else
{
    header('Content-type: text/xml');
    echo '<posts>';
    foreach ($posts as $index => $success)
    {
        if (is_array($success))
        {
            foreach ($success as $key => $value)
            {
                echo '<', $key, '>';
                if (is_array($value))
                {
                    foreach ($value as $tag => $val)
                    {
                        echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
                    }
                }
                echo '</', $key, '>';
            }
        }
    }
    echo '</posts>';
} 

?>

http://localhost/MyProject/user_signup/registration/Amit/Kumar/amit

The view is unnecessary for returning json. Just return json_encode($your_object) right from the controller.

Either way, the method you are looking for is json_encode().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM