【问题标题】:JSON.parse give me an error but JSONLint say it's a valid jsonJSON.parse 给我一个错误,但 JSONLint 说它是一个有效的 json
【发布时间】:2015-03-28 17:31:59
【问题描述】:

我正在尝试使用 jqplot 制作一个图形,通过 mysql ajax 提取值,我已经阅读了很多关于此的信息,我是 IT,对我来说,提出这个问题有点尴尬。这让我很头疼:(

我已经花了 20 天的时间解决了这个问题,但我不能独自一人,有人可以帮助我吗?我已经尝试了几乎所有的东西(json.parse、parsejson、getjson、datatype html、datatype json、方法 get 和 post.....)

我知道这很容易...... 为什么当我尝试执行 JSON.parse(resultado) 时会崩溃?

好像是[字符

这里是php:

<?php include('conex.php');
$datos=mysql_query("SELECT * FROM Meteorologia");
$arrDatos = array();
while ($rs=mysql_fetch_assoc($datos))
    $arrDatos[] = array_map('utf8_encode', $rs);
echo json_encode($arrDatos);
?>

我得到:

[{"FECHA":"2015-01-01","OZONO":"3","KT":"2","VV":"4"},{"FECHA":"2016-03-03","OZONO":"68","KT":"86","VV":"78"}]

这个js是:

try
{
    var strHtml = "";
    $.ajax({
            global: false,
            dataType: "html",
            async: false,
            type: "POST",
            url: $("#form").attr('action'),
            data: $("#form").serialize(),
            success: function(resultado){
                alert('success!!'+resultado);
                console.log(typeof resultado);
                console.log(resultado);
                //var datosRecibidos = JSON.parse(resultado);
                //var datosRecibidos = jQuery.parseJSON(resultado);
                var lista = "";
                $.each( resultado, function( key, value ) {
                        if(value.FECHA == "2015-01-01")
                        {
                            alert('si!!');
                        }
                        else
                        {
                            alert('sino!!');
                        }
                });
            },
            error: function(data){
                alert('Error!!: '+data);
            }
    });
}
catch(ex)
{
    alert("catch!!"+ex);
}

使用 $each 键值...我可以在数组内移动吗?

【问题讨论】:

  • 在致电JSON.parse() 之前添加console.log(typeof resultado)。我怀疑问题可能是响应已经被解析了。
  • 好的,如果它说它是一个字符串,接下来要做的是验证它是 正确 字符串。您应该能够在浏览器开发者控制台中看到 ajax HTTP 事务。使用 Firebug,如果响应是有效的 JSON,您甚至会看到一个 JSON 选项卡。
  • 这不是问题,但value.fecha 应该是value.FECHA
  • 他清楚地设置了数据类型'html',但无论如何都要随机猜测。
  • @Jhawins 我数不清有多少次发布的代码不是实际使用的代码,尝试重新解析已经解析的 JSON 是一个非常常见的错误。

标签: javascript php mysql ajax json


【解决方案1】:

在尝试了很多东西(更改数据类型 json、html、...、方法 get、post...)之后

我用地址文件调试:///C:/xampp/htdocs/traerdatos/index.html

而不是http://localhost/traerdatos/index.html

所以解决方案是将地址更改为本地主机

(感谢改革后的他帮助我实现了他的愿景)

新颖的错误 =)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多