julang 发表于 2010-5-2 22:41:05

关于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代码啊,怎么就是不行呢??? 哪位仁兄可以指点指点!! 被搞得头都大了!!!

Hex 发表于 2010-5-3 00:19:58

用 firebug 看一下输出有什么不同。

visvoy 发表于 2010-5-3 00:26:05

去了index.php没?如果没去掉,那post链接不正确

julang 发表于 2010-5-3 09:05:19

post链接没问题,返回值输出'{"result":"music"}',看上去也没问题,把这段输出直接放在JS脚本里就正常,但通过PHP返回就不行,真是纳闷啊。。。。

supercode 发表于 2010-5-13 18:39:19

在用flexigrid,把jquery 1.4死活不显示记录集,跟了半天,才发现是jquery 1.4对json格式的问题。

supercode 发表于 2010-5-13 21:05:05

看完这段的话,估计就全明白了
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后,不管是键还是值(字符型)都要用双引号。

feliving 发表于 2010-5-30 21:41:33

恩,上次碰到这个问题。

keyof 发表于 2010-6-4 08:48:56

新手,刚接触PHP框架

leiqing97 发表于 2012-2-23 09:44:46

正在搜集这方面的资料

huboo82 发表于 2012-2-23 12:00:57


function getlist()
{
   $arr = array('result' => $this->input->post('name'));
   header('Content-type: application/json; charset=UTF-8');
   echo json_encode($arr);
}

这样试试看呢?
页: [1]
查看完整版本: 关于jquery 1.4 json格式的问题