【发布时间】:2012-11-21 14:51:19
【问题描述】:
我向服务器发送和 ajax 请求,我想接收 json 响应,但我收到 html 响应,这段代码有什么问题?
//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);
}
响应是一个 html 页面而不是 json 对象,并且不会出现警告框。 当我删除 dataType:"json" 时,会出现警告框并包含 html 页面! 以及“var arr = jquery.parseJSON(result);”之后的任何代码不起作用,例如。警报(“嗨”); !
【问题讨论】:
-
html页面包含什么?
-
@ekims : html 页面是 WAMPSERVER 主页!!
-
你不需要使用
parseJSON(result):如果你使用type: "json"获取,result应该已经被解析了。 -
@itachi 我已经调试了 天 我疯狂的 HTML 响应而不是 JSON ......这个简单的建议帮助我解决了所有问题。谢谢。
标签: php ajax json codeigniter