嗨,我正在学习如何与Protovis合作,到目前为止一切都很好,但是现在我偶然发现了一个我似乎无法解决的问题。

以下是代码。 ((我的标题中有最新的jQuery)

<script type="text/javascript+protovis"> 
var dataURL = "http://eagereyes.org/media/2010/protovis-primer/earthquakes.json";

var JSONdata = $.ajax({ type: "GET", url: dataURL, async: false }).responseText;
var earthquakes = JSON.parse(JSONdata);

var width = 560;
var height = 245;

var barWidth = width/earthquakes.length;
var gap = 2;

new pv.Panel().width(width).height(height+5)
    .add(pv.Bar)
        .data(earthquakes)
        .bottom(0)
        .width(barWidth-gap)
        .height(function(d) d.Magnitude * (height/9))
        .left(function() this.index * barWidth)
    .root.render();

当我在Firefox中尝试此事时,我会得到此警报:

Syntax:Error JSON.parse

我已经验证了JSON http://www.jsonlint.com/ 已经。因此,问题必须在其他地方。

有人知道这里发生了什么吗?

编辑

我尝试在ProtoViewer应用中加载相同的数据: http://www.rioleo.org/protoviewer/ 它有效。因此,它必须是代码。

有帮助吗?

解决方案

您是否尝试过定期的异步回调,而不是同步方法?喜欢:

var dataURL = "http://eagereyes.org/media/2010/protovis-primer/earthquakes.json";

$.ajax({
  type: "GET",
  url: dataURL,
  success: function(response) {
    var earthquakes = JSON.parse(JSONdata);

    var width = 560;
    var height = 245;

    var barWidth = width/earthquakes.length;
    var gap = 2;

    new pv.Panel().width(width).height(height+5)
        .add(pv.Bar)
            .data(earthquakes)
            .bottom(0)
            .width(barWidth-gap)
            .height(function(d) d.Magnitude * (height/9))
            .left(function() this.index * barWidth)
        .root.render();     
  }
});

另外,该页面发出请求在地址栏中显示的页面上的JSON文件(完全是 http://eagereyes.org)?

最后,不需要手册json.parse()步骤。如果添加 dataType: 'json' 参数,$ .ajax()将自动应为json,并在可用的情况下使用json.parse()。

其他提示

添加半彩色 ; 回复结束

您正在使用哪个浏览器?有些浏览器没有定义 JSON 目的。您可以从下面的URL下载脚本,该脚本将定义 JSON 如果对象尚不存在。

https://github.com/douglascrockford/json-js

您可以检查是否 JSON 定义如下:

alert(JSON);

更新

好的,我要做的是检查Ajax调用实际上是返回CORECT数据。更改您的代码以打印从Ajax调用返回的JSON。

var JSONdata = $.ajax({ type: "GET", url: dataURL, async: false }).responseText;
alert(JSONdata);
var earthquakes = JSON.parse(JSONdata);
$.ajax({
            type: "POST",
            url: "saveChangesInEditing.php",
            data: idObject,
            success: function(data){
                         dataObject = JSON.parse(data);
                         $("input[name = 'id']").val(dataObject.id);
                         $("input[name='full_name']").val(dataObject.full_name);
                         $("input[name='sport']").val(dataObject.sport);
                         $("input[name='idol']").val(dataObject.idol);
                    },
            error: function(data){
                     alert("error!" + data);
                   }
        });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top