简体   繁体   English

在Codeigniter中创建Web服务时无法获取JSON格式的响应

[英]Cannot get response in json format when creating webservice in codeigniter

I am creating register webservice in Codeigniter. 我正在Codeigniter中创建注册Web服务。 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. 我想以json格式获取响应,如果注册成功,则将以json格式返回数据,如果数据已经存在,则将返回json响应。 I have confusion in how to pass value from controller to view and convert it into json response. 我对如何从控制器传递值以查看并将其转换为json响应感到困惑。 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): Signup_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 http:// localhost / MyProject / user_signup / registration / Amit / Kumar / amit

The view is unnecessary for returning json. 该视图对于返回json是不必要的。 Just return json_encode($your_object) right from the controller. 只需从控制器返回json_encode($ your_object)即可。

Either way, the method you are looking for is json_encode(). 无论哪种方式,您正在寻找的方法都是json_encode()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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