问题描述
我有以下有效的json数据
I have the following valid json data
[
{
"title":"title1",
"value":12234
},
{
"title":"title2",
"value":"some text"
},
{
"title":"title3",
"value":"12qwerty234"
},
{
"title":"title4",
"value":123.5
}
]
我使用jQuery加载下一个代码
I am using jQuery to load it with the next code
$(document).ready(function(){
$.getJSON("json.js", {},function(result){
$.each(result, function(i, obj) {
$("form").append($('<label for="'+i+'">'+obj.title+'</label>'));
$("form").append($('<input id="'+i+'" value="'+obj.value+'" type="text"/><br>'));
});
});
});
我的问题是,我在Firefox中收到语法错误。我加载json.json作为本地文件。
以下是屏幕截图
My problem is, that I am getting a syntax error in Firefox. I load json.json as a local file.Here is a screenshot
注意,表单已成功生成。
Note, that form has been generated successfully.
编辑:
这是Chrome运行python SimpleHTTPServer
:
Edit :Here is another screenshot from Chrome when running python SimpleHTTPServer
:
推荐答案
发生这种情况的原因是因为你使用的是一个本地文件,所以mime类型的text / xml因此Firefox将尝试将其解析为基础XHR对象的 .responseXML
中的XML。这当然失败。
The reason this happens is because you're using a local file, so a mime type of "text/xml" is implied and hence Firefox will try to parse it as XML into .responseXML
of the underlying XHR object. This of course fails.
您可以忽略这一点,或指定 mimeType
自己:
You may just ignore this, or specify the mimeType
yourself:
$.ajax({
dataType: "json",
url: "json.json",
mimeType: "application/json",
success: function(result){
$.each(result, function(i, obj) {
$("form").append($('<label for="'+i+'">'+obj.title+'</label>'));
$("form").append($('<input id="'+i+'" value="'+obj.value+'" type="text"/><br>'));
});
}
});
PS:使用纯XHR,您将使用
这篇关于有效JSON上的jQuery getJSON语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!