我使用以下命令拉出json数组:
$.get('URL')
.always(function(data) {
console.log(data);
直接访问URL时会返回以下信息:
{
"user": {
"ipaddr": "192.168.37.10.",
"system_date": "Feb 10 10:38:51 PST 2015"
},
"clean_graph_allow": 0,
"client": {
"account_information": {
0: {
"ID": 7611,
"accountID": "abcd",
"label": "Billing",
"email": "bob@jones.com",
"firstName": "Bob",
"lastName": "Jones",
"company": "Bob inc.",
"address": "123 Fake Street",
"city": "Nowhere",
"province": "CA",
"postalCode": 91111,
"country": "US",
"phone": 5555555555,
"notes": null,
"company_type": null
},
1: {
"ID": 7611,
"accountID": "abcd",
"label": "Billing",
"email": "bob@jones.com",
"firstName": "Bob",
"lastName": "Jones",
"company": "Bob inc.",
"address": "123 Fake Street",
"city": "Nowhere",
"province": "CA",
"postalCode": 91111,
"country": "US",
"phone": 5555555555,
"notes": null,
"company_type": null
},
2: {
"ID": 7611,
"accountID": "abcd",
"label": "Billing",
"email": "bob@jones.com",
"firstName": "Bob",
"lastName": "Jones",
"company": "Bob inc.",
"address": "123 Fake Street",
"city": "Nowhere",
"province": "CA",
"postalCode": 91111,
"country": "US",
"phone": 5555555555,
"notes": null,
"company_type": null
},
"contact": {
"ID": 7611,
"accountID": "abcd",
"label": "Billing",
"email": "bob@jones.com",
"firstName": "Bob",
"lastName": "Jones",
"company": "Bob inc.",
"address": "123 Fake Street",
"city": "Nowhere",
"province": "CA",
"postalCode": 91111,
"country": "US",
"phone": 5555555555,
"notes": null,
"company_type": null
}
},
"is_reseller": 300
},
"hidden_pages": [],
"action": "dashboard_api_stats",
"attacks_today": 4920,
"blocks": 1816,
"short_blocks": 101,
"nullroutes": 314,
"num_countries": 0,
"attack_dnsc": 3,
"attack_pstc": 6,
"attack_gc": 28,
"attack_core": 55,
"attack_tcpc": 8,
"attack_udpc": 0,
"regional": {
"": 100
}
}
我从$ .get或$ .getJSON命令返回的响应如下所示:
Object {
readyState: 4,
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
}
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: 4 responseText: "{"
user ":{"
ipaddr ":"
192.168.37.10.
","
system_date ":"
Feb 10 10: 38: 51 PST 2015 "},"
clean_graph_allow ":0,"
client ":{"
account_information ":{0:{"
ID ":7611,"
accountID ":"
abcd ","
label ":"
Billing ","
email ":"
bob@ jones.com ","
firstName ":"
Bob ","
lastName ":"
Jones ","
company ":"
Bob inc.
","
address ":"
123 Fake Street ","
city ":"
Nowhere ","
province ":"
CA ","
postalCode ":91111,"
country ":"
US ","
phone ":5555555555,"
notes ":null,"
company_type ":null},1:{"
ID ":7611,"
accountID ":"
abcd ","
label ":"
Billing ","
email ":"
bob@ jones.com ","
firstName ":"
Bob ","
lastName ":"
Jones ","
company ":"
Bob inc.
","
address ":"
123 Fake Street ","
city ":"
Nowhere ","
province ":"
CA ","
postalCode ":91111,"
country ":"
US ","
phone ":5555555555,"
notes ":null,"
company_type ":null},2:{"
ID ":7611,"
accountID ":"
abcd ","
label ":"
Billing ","
email ":"
bob@ jones.com ","
firstName ":"
Bob ","
lastName ":"
Jones ","
company ":"
Bob inc.
","
address ":"
123 Fake Street ","
city ":"
Nowhere ","
province ":"
CA ","
postalCode ":91111,"
country ":"
US ","
phone ":5555555555,"
notes ":null,"
company_type ":null},"
contact ":{"
ID ":7611,"
accountID ":"
abcd ","
label ":"
Billing ","
email ":"
bob@ jones.com ","
firstName ":"
Bob ","
lastName ":"
Jones ","
company ":"
Bob inc.
","
address ":"
123 Fake Street ","
city ":"
Nowhere ","
province ":"
CA ","
postalCode ":91111,"
country ":"
US ","
phone ":5555555555,"
notes ":null,"
company_type ":null}},"
is_reseller ":300},"
hidden_pages ":[],"
action ":"
dashboard_api_stats ","
attacks_today ":4920,"
blocks ":1816,"
short_blocks ":101,"
nullroutes ":314,"
num_countries ":0,"
attack_dnsc ":3,"
attack_pstc ":6,"
attack_gc ":28,"
attack_core ":55,"
attack_tcpc ":8,"
attack_udpc ":0,"
regional ":{"
":100}}"
setRequestHeader: function(a, b) {
var c = a.toLowerCase();
return t || (a = s[c] = s[c] || a, r[a] = b), this
}
state: function() {
return c
}
status: 200 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
}
statusText: "OK"
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
}
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
我为上述格式道歉。我不确定如何正确格式化。
无论如何,重要的部分是responseText。 responseText等效于直接访问URL时返回的数据。我需要能够从中提取特定元素(例如,attack_today)。
简单的事情:
data.responseText.attacks_today
OR
data['responseText']['attacks_today']
不要工作。我也尝试了各种循环方法,但它们都给了我相同类型的错误:
var arr = data.responseText;
$.each( arr, function( index, entry ) {
结果:
Uncaught TypeError: Cannot use 'in' operator to search for '1700' in ...
除非我错了,否则我必须采用一些简单的方法来访问json元素,但我只是没有看到它。
我正在使用jQuery版本1.11.0。
感谢。
答案 0 :(得分:1)
你的Ajax失败了。文档说,如果请求成功,第一个参数将是xhr对象或数据对象。
jqXHR.always(function(data | jqXHR,textStatus,jqXHR | errorThrown){ });
完整回调选项的替代构造, .always()方法替换了已弃用的.complete()方法。
响应成功的请求,函数的参数是 与.done():data,textStatus和jqXHR对象相同。对于 失败的请求参数与.fail()的参数相同: jqXHR对象,textStatus和errorThrown。参考deferred.always() 有关实施细节。
http://api.jquery.com/jquery.ajax/
您看到的对象是XHR对象,因为请求失败。如果你想确定在找出你的请求失败的原因之后总是得到结果,请使用.done(数据)。