关于jquery 1.4 json格式的问题
我在CI上用jquery 1.4.2,JS端:$.post("/mix/getlist", { 'name' : item },
function(data){
alert(data.result);
}, "JSON");
CI 端:
function getlist()
{
$arr = array('result' => $this->input->post('name'));
echo json_encode($arr);
}
以上代码很简单了,但是在JS端运行时会出错,提示data.result undefined。改用jquery 1.3.2正常,但1.4以上都不行,看文档说1.4对JSON格式严格,但这个php返回的也是很标准的json代码啊,怎么就是不行呢??? 哪位仁兄可以指点指点!! 被搞得头都大了!!! 用 firebug 看一下输出有什么不同。 去了index.php没?如果没去掉,那post链接不正确 post链接没问题,返回值输出'{"result":"music"}',看上去也没问题,把这段输出直接放在JS脚本里就正常,但通过PHP返回就不行,真是纳闷啊。。。。 在用flexigrid,把jquery 1.4死活不显示记录集,跟了半天,才发现是jquery 1.4对json格式的问题。 看完这段的话,估计就全明白了
Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.
在1.4后,不管是键还是值(字符型)都要用双引号。 恩,上次碰到这个问题。 新手,刚接触PHP框架 正在搜集这方面的资料
function getlist()
{
$arr = array('result' => $this->input->post('name'));
header('Content-type: application/json; charset=UTF-8');
echo json_encode($arr);
}
这样试试看呢?
页:
[1]