JSON response is empty in chrome console CodeIgniter

Mithun Shreevatsa

I am facing weird issue while accessing for json response.

When i access url : http://testproject/api/index.php/admin_customers/

JSON Response:

[{"name":'abc'}, {"name":'def'}, "name":'xyz'}]

But when i try to do the same from ajax with jqgrid, i am not receiving any data.

My Controller Code:

class Api extends CI_Controller {
  public function admin_customers(){
    $query = "select * from customer";
    $rows = $this->db->query($query);
    $array_rows = $rows->result();
    $json_data = json_encode($array_rows);
    header('Content-Type: application/json');
    echo $json_data;
  }
}

No luck even after tried as:

return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output($json_data);

Output Response In Console:

enter image description here

My jQgrid JavaScript Code is:

   $("#data-grid").jqGrid({
        url:'http://localhost/testproject/index.php/api/admin_customers/',
        mtype: "GET",
        datatype: "json",
        colModel: [
            { label: 'Name', name: 'name', key: true, width: 75, editable: true}
        ],
        editurl:'http://localhost/testproject/api/admin_customers_edit/',
        viewrecords: true,
        height: 250,
        rowNum: 20,
        pager: "#jqGridPager"
    });

What am i doing wrong?

No matter of jQgrid, it is not displayed in response itself in chrome console.

Mithun Shreevatsa

@Oleg was rite, it was cross origin scripting error. So i finally managed to do it by allowing cross origin scripting by sending extra header content as below:

   public function admin_customers(){
        $query = "select * from customer";
        $rows = $this->db->query($query);
        $data = $rows->result();
        header('Content-type: application/json');
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: GET");
        header("Access-Control-Allow-Methods: GET, OPTIONS");
        header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
        echo json_encode($data, JSON_NUMERIC_CHECK);
    }

This solved my problem finally :)

But this is security issues. We have to authorize the tokens here as well if i am not wrong.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSON response is empty in chrome console CodeIgniter

From Dev

Get JSON response In Codeigniter

From Dev

JSON response for empty params

From Dev

Ajax Call empty response on Google chrome Mobile

From Dev

Chrome is not rendering chunked json response

From Dev

CodeIgniter - How to return Json response from controller

From Dev

Append HTML Table To Json Response In Codeigniter

From Dev

codeigniter-restserver how to put json into the response

From Dev

codeigniter curl loop over json response

From Dev

Hitting directly a controller in codeigniter and getting json response

From Dev

arrow functions and logging json response to console

From Dev

Getting different JSON response in browser and Xcode console

From Dev

Django; error on printing JSON response in Firefox console

From Dev

Jersey - JSON response empty if polymorphism used

From Dev

Ajax : empty json response when using Karma

From Dev

How to detect an empty element of a JSON response

From Dev

RX Alamofire Swift empty response JSON

From Dev

Constructing json response. NodeJS send empty response

From Dev

jQuery ajax fails *sometimes* (ERR_EMPTY_RESPONSE) in Chrome and Safari

From Java

How to find out json response is an empty JSON object in android?

From Dev

How to find out json response is an empty JSON object in android?

From Dev

how to get element from json_encode array response - Codeigniter

From Dev

Uncaught SyntaxError: Unexpected token < in JSON at position 0 from Codeigniter response

From Dev

AJAX response returns valid JSON, console logs undefined

From Dev

ASP.NET Web API returns empty collection in json response

From Dev

Empty JSON response doesn't trigger error in Jackson

From Dev

Return empty JSON from spring controller for void response

From Dev

symfony/FOSRestBundle : empty JSON response (using the symfony embodied serializer)

From Dev

cant parse json response, its actually not empty yet returns undefined

Related Related

  1. 1

    JSON response is empty in chrome console CodeIgniter

  2. 2

    Get JSON response In Codeigniter

  3. 3

    JSON response for empty params

  4. 4

    Ajax Call empty response on Google chrome Mobile

  5. 5

    Chrome is not rendering chunked json response

  6. 6

    CodeIgniter - How to return Json response from controller

  7. 7

    Append HTML Table To Json Response In Codeigniter

  8. 8

    codeigniter-restserver how to put json into the response

  9. 9

    codeigniter curl loop over json response

  10. 10

    Hitting directly a controller in codeigniter and getting json response

  11. 11

    arrow functions and logging json response to console

  12. 12

    Getting different JSON response in browser and Xcode console

  13. 13

    Django; error on printing JSON response in Firefox console

  14. 14

    Jersey - JSON response empty if polymorphism used

  15. 15

    Ajax : empty json response when using Karma

  16. 16

    How to detect an empty element of a JSON response

  17. 17

    RX Alamofire Swift empty response JSON

  18. 18

    Constructing json response. NodeJS send empty response

  19. 19

    jQuery ajax fails *sometimes* (ERR_EMPTY_RESPONSE) in Chrome and Safari

  20. 20

    How to find out json response is an empty JSON object in android?

  21. 21

    How to find out json response is an empty JSON object in android?

  22. 22

    how to get element from json_encode array response - Codeigniter

  23. 23

    Uncaught SyntaxError: Unexpected token < in JSON at position 0 from Codeigniter response

  24. 24

    AJAX response returns valid JSON, console logs undefined

  25. 25

    ASP.NET Web API returns empty collection in json response

  26. 26

    Empty JSON response doesn't trigger error in Jackson

  27. 27

    Return empty JSON from spring controller for void response

  28. 28

    symfony/FOSRestBundle : empty JSON response (using the symfony embodied serializer)

  29. 29

    cant parse json response, its actually not empty yet returns undefined

HotTag

Archive