简体   繁体   English

按名称/键回调json值

[英]callback json value by name/key

With this function I'm trying to get the licence name from a json url by name/key my json looks like this: 使用此功能,我试图通过名称/密钥从json URL获取许可证名称,我的json如下所示:

[{"Gallery":true,"Responsive":true,"Main":true,"Seasonal":true}]

js: 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
});

What I need is to get the true or false value using something like this 我需要的是获得truefalse方法是使用下面的值

getLicenceName(Gallery);

I need to use it in my functions eg: if(getLicenceName(Gallery)=false)... 我需要在我的函数中使用它,例如: 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
});

Will do the trick. 会成功的。

You can't really do if(getLicenceName(Gallery) == false) because the AJAX request is asynchronous, but you can do it this way: 您实际上不能执行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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM