I’m trying to get the data through ajax for tagify whitelist. but I get the following mistake
JavaScript
x
2
1
ReferenceError: Can't find variable: clist
2
code is:
JavaScript
1
27
27
1
$.ajax({
2
url: '/ajaxget/tags',
3
method: 'GET',
4
data: {
5
<?= csrf_token() ?> : '<?=csrf_hash()?>'
6
},
7
success: function(response) {
8
var clist = response;
9
//alert(response);
10
}
11
});
12
13
14
var input = document.querySelector('input[name="tags"]');
15
16
tagify = new Tagify(input, {
17
enforceWhitelist: true,
18
whitelist: clist,
19
maxTags: 5,
20
dropdown: {
21
maxItems: 5,
22
classname: "tags-look",
23
enabled: 0,
24
closeOnSelect: false
25
}
26
});
27
when I test it with “alert (response);” displays the data – ['123','333','763',asd']
Advertisement
Answer
You are trying to access a local variable from callback response as global variable.
JavaScript
1
28
28
1
$.ajax({
2
url: '/ajaxget/tags',
3
method: 'GET',
4
data: {
5
<?= csrf_token() ?> : '<?=csrf_hash()?>'
6
},
7
success: function(response) {
8
var clist = response;
9
populateList(clist);
10
}
11
});
12
13
function populateList(clist) {
14
var input = document.querySelector('input[name="tags"]');
15
16
tagify = new Tagify(input, {
17
enforceWhitelist: true,
18
whitelist: clist,
19
maxTags: 5,
20
dropdown: {
21
maxItems: 5,
22
classname: "tags-look",
23
enabled: 0,
24
closeOnSelect: false
25
}
26
});
27
}
28