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:
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.
@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.
Comments