我目前正在做关于 Jquery、Ajax 和 JSON 的练习。我找到了几种获取数据的方法,但现在我想将这些数据从 JSON 存储到一个变量中。据我所知,JSON 只是一大堆对象。
我的 JSON 文件如下所示:
{
"Products" : [{
"Id": 11,
"Name": "Keyboard",
"Description": "Microsoft Keyboard",
"PriceExVat": 199.95,
"QtyInStock": 11
},
{
"Id": 211,
"Name": "Mouse",
"Description": "Microsoft 3 button Mouse with scroller",
"PriceExVat": 199.95,
"QtyInStock": 30
},
{
"Id": 35,
"Name": "TowerCase",
"Description": "Mini Tower Case with 450W power unit",
"PriceExVat": 600.95,
"QtyInStock": 23
},
{
"Id": 58,
"Name": "Monitor",
"Description": "17inch LCD monitor",
"PriceExVat": 1499.95,
"QtyInStock": 12
}]
}
我的 Jquery 代码如下所示:
var products;
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'data/Products.json',
data: { get_param: 'value'},
dataType: 'json',
complete: function(data){
products = data; //Store JSON data
}
});
});
$(document).ajaxComplete(function(){
alert(products); //returns object [Object]. When attempting to get data here like products[0].Name, an error occurs saying that Name does not exist
});
我之所以这样做,是因为我知道 ajax 是异步javascript 和 xml。因此,如果我使用success : function(){...}
,将无法保证我的数据将保存到变量中products
(即时间问题)。
所以我设置了ajax的代码,在操作完成时存储数据。这将触发ajaxComplete()
事件,理论上允许我访问我保存到变量中的数据。
不过这里有一个小问题,也是我提出问题的原因。如果我alert(products)
,我得到回应
稍微改变我的代码(因为现在我认为应该在那个变量中存储一些东西),到alert(products.Products[0].Name);
,抛出一个错误(暗示这个变量我是未定义的)
有没有人可以传递给我的建议让我遵循以获取数据?这个变量的目标是我想用它来分页到我的表中,并且通过使用存储在变量中的数据来减少对 json 文件(以后可能成为远程托管文件)的调用量。
编辑 更改警报到控制台。日志
对象 {readyState: 4, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}
不是我希望看到的。
编辑 显然我什至没有正确显示日志。
Object {readyState: 4, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}
abort: function (a){a=a||"abort",p&&p.abort(a),w(0,a);return this}
always: function (){i.done.apply(i,arguments).fail.apply(i,arguments);return this}
complete: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}
done: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}
error: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}
fail: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}
getAllResponseHeaders: function (){return s===2?n:null}
getResponseHeader: function (a){var c;if(s===2){if(!o){o={};while(c=bG.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c}
isRejected: function (){return!!i}
isResolved: function (){return!!i}
overrideMimeType: function (a){s||(d.mimeType=a);return this}
pipe: function (a,b,c){return f.Deferred(function(d){f.each({done:[a,"resolve"],fail:[b,"reject"],progress:[c,"notify"]},function(a,b){var c=b[0],e=b[1],g;f.isFunction(c)?i[a](function(){g=c.apply(this,arguments),g&&f.isFunction(g.promise)?g.promise().then(d.resolve,d.reject,d.notify):d[e+"With"](this===i?d:this,[g])}):i[a](d[e])})}).promise()}
progress: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}
promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a}
readyState: 4
responseText: "{
↵ "Products" : [{
↵ "Id": 11,
↵ "Name": "Keyboard",
↵ "Description": "Microsoft Keyboard",
↵ "PriceExVat": 199.95,
↵ "QtyInStock": 11
↵ },
↵ {
↵ "Id": 211,
↵ "Name": "Mouse",
↵ "Description": "Microsoft 3 button Mouse with scroller",
↵ "PriceExVat": 199.95,
↵ "QtyInStock": 30
↵ },
↵ {
↵ "Id": 35,
↵ "Name": "TowerCase",
↵ "Description": "Mini Tower Case with 450W power unit",
↵ "PriceExVat": 600.95,
↵ "QtyInStock": 23
↵ },
↵ {
↵ "Id": 58,
↵ "Name": "Monitor",
↵ "Description": "17inch LCD monitor",
↵ "PriceExVat": 1499.95,
↵ "QtyInStock": 12
↵ },
↵ {
↵ "Id": 234,
↵ "Name": "Laptop",
↵ "Description": "Acer Core I5 Laptop",
↵ "PriceExVat": 6999.95,
↵ "QtyInStock": 7
↵ },
↵ {
↵ "Id": 789,
↵ "Name": "CarryCase",
↵ "Description": "Targus Carry Case",
↵ "PriceExVat": 399.95,
↵ "QtyInStock": 20
↵ },
↵ {
↵ "Id": 7,
↵ "Name": "Harddrive",
↵ "Description": "1TB External Hard Drive",
↵ "PriceExVat": 999.95,
↵ "QtyInStock": 100
↵ },
↵ {
↵ "Id": 51,
↵ "Name": "Projector",
↵ "Description": "HD Projector",
↵ "PriceExVat": 4995.95,
↵ "QtyInStock": 1
↵ },
↵ {
↵ "Id": 901,
↵ "Name": "Joystick",
↵ "Description": "Microsoft Joystick",
↵ "PriceExVat": 400.95,
↵ "QtyInStock": 2
↵ },
↵ {
↵ "Id": 500,
↵ "Name": "USBCable",
↵ "Description": "3m USB-toUSB cable",
↵ "PriceExVat": 80.95,
↵ "QtyInStock": 5
↵ }]
↵}"
setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}
state: function (){return e}
status: 200
statusCode: function (a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this}
statusText: "OK"
success: function (){if(c){var a=c.length;n(arguments),j?l=c.length:e&&e!==!0&&(k=a,o(e[0],e[1]))}return this}
then: function (a,b,c){i.done(a).fail(b).progress(c);return this}
__proto__: Object