javascript - javascript Codeigniter 中的 Json 对象

标签 javascript json codeigniter

这是调用 Controller 中函数的 Jquery。但它无法读取 Json_encoded 对象。我正在使用代码点火器。为什么?

 $(this).ajaxSubmit({ 
                    type : "POST",
                    url: '../index.php/user/signin',   // target element(s) to be updated with server response 
                    cache : false,
                    dataType:'json',
                    success : function(data){ 
                        data = $.trim(data);
                        alert(data);
                        var obj = jQuery.parseJSON(data);

                        alert(obj.Condition);
                    },
                    error: function(){

                    }

Controller

$arr = array('Condition' => $condition, 'Message' => $msg);

// header('Content-Type: application/json');
echo json_encode($arr); 

最佳答案

尝试使用eval代替parseJSON

在js中:

  $(this).ajaxSubmit({ 
                type : "POST",
                url: '../index.php/user/signin',   // target element(s) to be updated with server response 
                cache : false,
                dataType:'json',
                success : function(data){ 
                    data = $.trim(data);
                    alert(data);
                    var obj = eval('(' + data+ ')');

                    alert(obj.Condition);
                },
                error: function(){

                }

我还认为没有必要使用 $.trim 来处理响应数据

关于javascript - javascript Codeigniter 中的 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822625/

相关文章:

javascript - 设置谷歌地图标记javascript

javascript - 从元素 jQuery 获取内联 css 宽度属性

json - Swift:将结构转换为 JSON?

python - 需要帮助在 python 中附加字典项目

javascript - 单击链接javascript,codeigniter时如何更改事件类

javascript - 如何声明一个 Map<T, K> ,其中 T 应该是可变参数类而 K 应该是 T 的实例?

javascript - 我将如何在纯 JavaScript 中暂时禁用该事件?

python - 尝试使用 Python 打印 JSON 数据

mysql - CodeIgniter - 数据库事务

CakePHP 与 codeigniter - 我在 CakePHP 方面经验丰富