javascript 语法:错误 JSON.parse,当尝试为 protovis 加载数据时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax:Error JSON.parse, When trying to load data for protovis
提问by bottleboot
Hi I'm learning how to work with protovis, so far so good, but now I stumbled upon a problem I can't seem to solve.
嗨,我正在学习如何使用 protovis,到目前为止还不错,但现在我偶然发现了一个我似乎无法解决的问题。
The following is the code. (I have the latest jquery loaded in my headers)
以下是代码。(我的标题中加载了最新的 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();
When I try this in Firefox i get this alert:
当我在 Firefox 中尝试此操作时,我收到此警报:
Syntax:Error JSON.parse
I have validated the JSON on http://www.jsonlint.com/already. So the problem must be elsewhere.
我已经在http://www.jsonlint.com/上验证了 JSON 。所以问题一定在别处。
Anyone knows whats going on here?
有谁知道这里发生了什么?
Edit
编辑
I tried loading the same data in the protoviewer app: http://www.rioleo.org/protoviewer/and it works. So it must be the code.
我尝试在 protoviewer 应用程序中加载相同的数据:http://www.rioleo.org/protoviewer/ 并且它有效。所以它必须是代码。
采纳答案by Dave Ward
Have you tried a regular async callback instead of the synchronous approach? Like:
您是否尝试过常规异步回调而不是同步方法?喜欢:
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();
}
});
Also, is that JSON file located on the same server that the page making the request shows in the address bar (exactly http://eagereyes.org)?
此外,该 JSON 文件是否位于与发出请求的页面显示在地址栏中的同一台服务器上(正好是http://eagereyes.org)?
Finally, the manual JSON.parse() step isn't necessary. If you add the dataType: 'json'parameter, $.ajax() will automatically deserialize as JSON and uses JSON.parse() where available.
最后,不需要手动 JSON.parse() 步骤。如果添加dataType: 'json'参数,$.ajax() 将自动反序列化为 JSON 并在可用的情况下使用 JSON.parse()。
回答by Marejean
$.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);
}
});
回答by Andy Skirrow
Which browser are you using? Some browsers don't define the JSONobject. You can download a script from the URL below which will define the JSONobject if it doesn't already exist.
您使用的是哪个浏览器?一些浏览器不定义JSON对象。您可以从下面的 URL 下载脚本,JSON如果对象不存在,它将定义该对象。
https://github.com/douglascrockford/JSON-js
https://github.com/douglascrockford/JSON-js
You can check whether JSONis defined as follows:
您可以检查是否JSON定义如下:
alert(JSON);
update
更新
OK next thing I'd do is check that the ajax call is actually returning the corect data. Change your code to print the JSON returned from the ajax call.
好的,接下来我要做的是检查 ajax 调用是否实际返回了正确的数据。更改您的代码以打印从 ajax 调用返回的 JSON。
var JSONdata = $.ajax({ type: "GET", url: dataURL, async: false }).responseText;
alert(JSONdata);
var earthquakes = JSON.parse(JSONdata);
回答by Diodeus - James MacFarlane
Add a semi-colon ;to the end of your response
;在回复的末尾添加分号

