Bryan Learn
Bryan Learn

Reputation: 1623

How to return JSON response from controller

How do I return response from the controller back to the Jquery Javascript?

Javascript

$('.signinform').submit(function() { 
   $(this).ajaxSubmit({ 
       type : "POST",
       url: 'index.php/user/signin', // target element(s) to be updated with server response 
       cache : false,
       success : onSuccessRegistered,
       error: onFailRegistered
   });        
   return false; 
}); 

Data is returned null (blank)!

function onSuccessRegistered(data){
    alert(data);
};

Controller -

public function signin() {
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    
    echo json_encode( $arr );
}

Upvotes: 42

Views: 167457

Answers (8)

rikhzan amir
rikhzan amir

Reputation: 1

For codeigniter 3+ use this to return output as json. Put this code in controller.

return $this->output->set_output(json_encode($data));

For codeigniter 4+ you can use this.

return $this->respond(json_encode($data), 200);

Hope this help

Upvotes: 0

Sohan Arafat
Sohan Arafat

Reputation: 93

If anyone is looking for a 4.3.x version solution it is this:

return $this->response->setJSON($data);

Source: https://codeigniter4.github.io/userguide/outgoing/response.html#setting-the-output

Upvotes: 0

cdsaenz
cdsaenz

Reputation: 550

In Codeigniter 4.3, I've used this within a before Filter, that doesn't have a $this->response as Controllers do:

return response()->setContentType('application/json')                             
                 ->setStatusCode(401)
                 ->setJSON(['error' => 'Access Denied']);

Upvotes: 0

Cliff Richard Anfone
Cliff Richard Anfone

Reputation: 1446

return $this->output
    ->set_content_type('application/json')
    ->set_status_header(500)
    ->set_output(json_encode([
        'text' => 'Error 500',
        'type' => 'danger'
    ]));

Upvotes: 96

Ghazaleh Javaheri
Ghazaleh Javaheri

Reputation: 2117

in my case , I'm using ci4 , I send response to clinet like this: e.g in App\Controllers\Category:setOrder my Category controller extends BaseController

  return $this->response->setJson(['msg'=>'update-success']);

Upvotes: 6

Player1
Player1

Reputation: 3166

For CodeIgniter 4, you can use the built-in API Response Trait

Here's sample code for reference:

<?php namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Home extends BaseController
{
    use ResponseTrait;

    public function index()
    {
        $data = [
            'data' => 'value1',
            'data2' => 'value2',
        ];

        return $this->respond($data);
    }
}

Upvotes: 6

Sundar
Sundar

Reputation: 4650

//do the edit in your javascript

$('.signinform').submit(function() { 
   $(this).ajaxSubmit({ 
       type : "POST",
       //set the data type
       dataType:'json',
       url: 'index.php/user/signin', // target element(s) to be updated with server response 
       cache : false,
       //check this in Firefox browser
       success : function(response){ console.log(response); alert(response)},
       error: onFailRegistered
   });        
   return false; 
}); 


//controller function

public function signin() {
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);    

   //add the header here
    header('Content-Type: application/json');
    echo json_encode( $arr );
}

Upvotes: 53

Sundar
Sundar

Reputation: 4650

This is not your answer and this is an alternate way to process the form submission

$('.signinform').click(function(e) { 
      e.preventDefault();
      $.ajax({
      type: "POST",
      url: 'index.php/user/signin', // target element(s) to be updated with server response 
      dataType:'json',
      success : function(response){ console.log(response); alert(response)}
     });
}); 

Upvotes: 0

Related Questions