JSON未由$ .getJSON捕获 [英] JSON Not grabbed by $.getJSON
问题描述
最终解决方案:我的.json
文件的格式比我在问题中包含的代码片段更深处存在格式错误.现在一切正常.非常感谢大家的评论,我将@TWLATL的答案标记为正确的,因为它是一种格式正确的函数解决方案,我以后可以在项目中使用.
Final solution: There was something wrong with the formatting of my .json
file deeper down than the snippet I included in the question. Everything is working now. Big thanks to everyone in the comments and I'm marking @TWLATL's answer as correct because it's a nicely formatted function solution I can use in projects later.
我正在尝试从script.js
(都在root
目录中)访问我的data.json
文件,它似乎根本没有运行.在进行错误处理之前,我没有任何迹象表明该功能已运行.多亏了评论,我用.fail
替换了.error
,但是我仍然不知道为什么它失败了.
I'm trying to access my data.json
file from script.js
(both in the root
directory) and it doesn't seem to be running at all. Before I put in error handling, I was getting no indication the function ran. Thanks to comments, I replaced .error
with .fail
, but I still can't figure out why it's failing.
index.html
<body>
<h1>JSON Render</h1>
<div id="fileViewer"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="script.js"></script>
</body>
script.js
$.getJSON('data.json', function(data) {
console.log('json grabbed')
console.log(data);
})
.fail(function(data) {
console.log("GetJSON Error!");
console.log(data);
});
$.ajax({
url: 'data.json',
dataType: 'json',
success: function( data ) {
console.log( "SUCCESS: " );
console.log( data );
},
error: function( data ) {
console.log( "AJAX ERROR: " );
console.log( data );
}
});
data.json(示例)
{
"children": [
{
"name": "Main Folder",
"type": "folder"
},
{
"name": "Private folder",
"type": "folder"
}
]
}
我一直在寻找Stack Overflow,但没有找到解决方案可以解决我的问题.救命!
I've been looking around on Stack Overflow and no solutions I've found seem to solve my problem. Help!
向我的index.html
添加了jQuery CDN.在那里.我只是忘了把它放在问题中了.
Added jQuery CDN to my index.html
. It was there. I just forgot to put it in the question.
更新了script.js以反映返回以下屏幕截图的代码. (屏幕截图在对象中间被截断.如果对象的上下文确实相关,则可以添加更多内容.)
Edit 2: Updated script.js to reflect the code that is returning the screenshots below. (screenshot cut off in the middle of the object. I can include more if the context of the object is actually relevant.)
新的AJAX错误处理: 新的ajax函数:
Edit 3: New AJAX Error handling: The new ajax function:
$.ajax({
url: 'data.json',
dataType: 'json',
success: function( data ) {
console.log( "SUCCESS: " );
console.log( data );
},
error: function(data, textStatus, errorThrown) {
console.log("textStatus: ",textStatus);
console.log("errorThrown: ",errorThrown);
}
});
现在我很好地获得了这个控制台输出:
And I am now wonderfully getting this console output:
推荐答案
使用经过稍微修改的代码版本,我可以很好地返回JSON:
Using a slightly modified version of your code, I have it returning the JSON just fine:
http://plnkr.co/edit/DMLyJa1Quj7ofszn5Y7o?p=info
$(document).ready(function() {
console.log("ready!");
var items;
$.getJSON('data.json', function(data) {
console.log('json grabbed');
})
.done(function(data) {
items = data.children;
console.log(items);
})
.fail(function(data) {
console.log("Error!");
});
}); // close document.ready
jQuery现在将.done用作成功方法.在该块中,只需将data.children项目分配给一个变量,就可以了.
Jquery uses .done now for the success method. In that block just assign your data.children item to a variable and you are good to go.
这篇关于JSON未由$ .getJSON捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!