javascript - getJson 返回所有 Handler 而不是数据

标签 javascript jquery getjson

我正在尝试从 html 应用程序中的文件系统加载 config.json:

var data = $.getJSON('../../modules/config.json');
console.log(data);

但是我没有收到来自 json 文件的数据(路径是正确的,我检查了两次),而是得到了这个:

Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}abort: function (a){var b=a||u;return i&&i.abort(b),x(0,b),this}always: function (){return e.done(arguments).fail(arguments),this}complete: function (){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}done: function (){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}arguments: nullcaller: nulllength: 0name: ""prototype: m.Callbacks.k.add__proto__: function Empty() {}<function scope>error: function (){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}fail: function (){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}getAllResponseHeaders: function (){return 2===t?f:null}getResponseHeader: function (a){var b;if(2===t){if(!j){j={};while(b=Cc.exec(f))j[b[1].toLowerCase()]=b[2]}b=j[a.toLowerCase()]}return null==b?null:b}overrideMimeType: function (a){return t||(k.mimeType=a),this}pipe: function (){var a=arguments;return m.Deferred(function(c){m.each(b,function(b,f){var g=m.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&m.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()}progress: function (){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}promise: function (a){return null!=a?m.extend(a,d):d}readyState: 1setRequestHeader: function (a,b){var c=a.toLowerCase();return t||(a=s[c]=s[c]||a,r[a]=b),this}state: function (){return c}statusCode: function (a){var b;if(a)if(2>t)for(b in a)q[b]=[q[b],a[b]];else v.always(a[v.status]);return this}success: function (){if(h){var d=h.length;!function f(b){m.each(b,function(b,c){var d=m.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}arguments: nullcaller: nulllength: 0name: ""prototype: m.Callbacks.k.add__proto__: function Empty() {}<function scope>then: function (){var a=arguments;return m.Deferred(function(c){m.each(b,function(b,f){var g=m.isFunction(a[b])&&a[b];e[f[1]](function(){var a=g&&g.apply(this,arguments);a&&m.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f[0]+"With"](this===d?c.promise():this,g?[a]:arguments)})}),a=null}).promise()}__proto__: Object

它似乎都是处理程序,但我只想从 json.file 获取数据。我想我只是错过了一件很小的事情。

最佳答案

getJson 函数返回一个 XHR(这是您在控制台中看到的),而不是请求收到的数据。要获取需要使用回调参数的数据:

$.getJSON('../../modules/config.json', function(data) {
    console.log(data);
});

关于javascript - getJson 返回所有 Handler 而不是数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897225/

相关文章:

jQuery:如何计算 "display"不是 "none"的元素数量?

jquery - 如何仅在批量 getJSON 指令完成时触发某些操作?

javascript - 无法从 json-p 调用访问数据

javascript - 未捕获的类型错误 : Cannot read property of null

javascript - jquery自定义事件困惑

jquery - 为什么 bottle 中的 request.json 返回 None?

javascript - css 样式不工作和 js

arrays - Hive 表数组列 - 使用 array_index 展开

JavaScript 回调范围

javascript - JS 切换不起作用