我正在嘗試通過 ajax 獲取資料以進行標記白名單。但我得到以下錯誤
ReferenceError: Can't find variable: clist
代碼是:
$.ajax({
url: '/ajaxget/tags',
method: 'GET',
data: {
<?= csrf_token() ?> : '<?=csrf_hash()?>'
},
success: function(response) {
var clist = response;
//alert(response);
}
});
var input = document.querySelector('input[name="tags"]');
tagify = new Tagify(input, {
enforceWhitelist: true,
whitelist: clist,
maxTags: 5,
dropdown: {
maxItems: 5,
classname: "tags-look",
enabled: 0,
closeOnSelect: false
}
});
當我用“警報(回應)”進行測驗時;顯示資料 -['123','333','763',asd']
uj5u.com熱心網友回復:
您正在嘗試從回呼回應中訪問區域變數作為全域變數。
$.ajax({
url: '/ajaxget/tags',
method: 'GET',
data: {
<?= csrf_token() ?> : '<?=csrf_hash()?>'
},
success: function(response) {
var clist = response;
populateList(clist);
}
});
function populateList(clist) {
var input = document.querySelector('input[name="tags"]');
tagify = new Tagify(input, {
enforceWhitelist: true,
whitelist: clist,
maxTags: 5,
dropdown: {
maxItems: 5,
classname: "tags-look",
enabled: 0,
closeOnSelect: false
}
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/343683.html
標籤:javascript 阿贾克斯 标记
上一篇:為什么我的sessionStorage給出{'objectObject':''}
下一篇:ajax沒有得到值?
