[英]callback json value by name/key
使用此功能,我試圖通過名稱/密鑰從json URL獲取許可證名稱,我的json如下所示:
[{"Gallery":true,"Responsive":true,"Main":true,"Seasonal":true}]
js:
function getLicenseName(name, callback){
var license = 'default'
$.getJSON(adl+'DesignTemplateBuilder.aspx?GetLicense=1', function(data){
/*
licence = data[0].Gallery;
respValue = data[0].Responsive;
seasonalValue = data[0].Seasonal;
*/
licence = data[0].name;
callback(licence)
});
}
getLicenseName(name, function(Responsive) {
console.log(name);
//this returns empty right now
});
我需要的是獲得true
或false
方法是使用下面的值
getLicenceName(Gallery);
我需要在我的函數中使用它,例如: if(getLicenceName(Gallery)=false)...
function getLicenseName(callback){
$.getJSON(adl+'DesignTemplateBuilder.aspx?GetLicense=1', function(data){
callback(data)
});
}
getLicenseName(function(data) {
console.log(data[0].Gallery);
//this returns empty right now
});
會成功的。
您實際上不能執行if(getLicenceName(Gallery) == false)
因為AJAX請求是異步的,但是您可以這樣進行:
function getLicenseName(name, callback) {
$.getJSON(adl+'DesignTemplateBuilder.aspx?GetLicense=1', function(data){
// pass back the name parameter
callback(data[0][name])
});
}
// use quotes around the name
getLicenseName('Gallery', function (name) {
if (name) {
...
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.