Posted inTechnology / Codeigniter / php / Programming

Return JSON Response in CodeIgniter controller

Today, We want to share with you Return JSON Response in CodeIgniter controller.In this post we will show you send json response codeigniter example, hear for codeigniter json encode array we will give you demo and example for implement.In this post, we will learn about codeigniter json response with an example.

In CodeIgniter, you can return a JSON response from a controller using the json method provided by the output class. Here’s an example:

public function getUser($id)
{
    $user = $this->User_model->getUserById($id);
    if (!$user) {
        // Return a JSON response with a 404 status code if the user is not found
        return $this->output
            ->set_status_header(404)
            ->set_content_type('application/json')
            ->set_output(json_encode([
                'error' => 'User not found'
            ]));
    }

    // Return a JSON response with the user data
    return $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($user));
}

In this example, the getUser method retrieves a user from the database using the User_model class. If the user is not found, we return a JSON response with a 404 status code and an error message. Otherwise, we return a JSON response with the user data.

To set the content type of the response to JSON, we use the set_content_type method provided by the output class. We also use the json_encode function to convert the data to a JSON string.

Note that there are many other ways to return a JSON response in CodeIgniter, and the exact approach will depend on your specific use case and requirements.

Return JSON Response in CodeIgniter controller

There are the Following The simple About Return JSON Response in CodeIgniter controller Full Information With Example and source code.

As I will cover this Post with live Working example to develop get json response php codeigniter, so the codeigniter return json header for this example is following below.

Json Response

public function returnMyProductJson() { 
  header('Content-Type: application/json');
  $productArray = ['id'=>9898, 'name'=>'Mobile'];
  echo json_encode($success);
} 

Completed Source Code

load->helper('url');

  // load view
  $this->load->view('product_view');
 }

 public function productDetails(){
  // POST data
  $postProductData = $this->input->post();

  //load model
  $this->load->model('Main_model');

  // get data
  $data = $this->Main_model->getProductDetails($postProductData);

  echo json_encode($data);
 }

}
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to send json response from controller in codeigniter ?.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype