I send and ajax request to the server and I want to receive json response but instead, I receive html response, what is the wrong in this code?
//jquery code
$('select[name=category]').click(function(){
$.ajax({
url: "/index.php/category/get_categories",
type: "post",
dataType: "json",
cache: false,
success: function (result) {
var arr = jquery.parseJSON(result);
alert(arr);
}
});
});
//php code
public function get_categories(){
$data = $this->category_model->get_cats_names_ids();
echo json_encode($data);
}
the response is an html page instead of json object and the alert box dose not appear. when I remove dataType:"json", the alert box appear and contain html page ! and any code after "var arr = jquery.parseJSON(result);" does not work, ex. alert("hi"); !
parseJSON(result): if you fetched withtype: "json", theresultshould already be parsed.