问题
I'm using the following Javascript to get data via PHP (upload.php) -
<script src="js/jquery.amsify.suggestags.js"></script>
<script>
fetch('./get_hashtags.php').then(response => {
return response.json();
}).then(data => {
var suggestionsArr = [];
data.forEach(function(item){
suggestionsArr.push(item.category);
});
console.log(suggestionsArr);
$('#inputTags').amsifySuggestags({
suggestions: suggestionsArr,
classes: ['bg-warning','bg-warning'],
});
}).catch(err => {
console.log(err);
});
</script>
get_hashtags.php
<?php
// ... some additional database connection details ...
$statement = $pdo->prepare("SELECT category FROM hashtags where active=1");
$hashtags = array();
if ($statement->execute()) {
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$hashtags[] = $row;
}
}
$pdo = null;
echo json_encode($hashtags);
?>
Unfortunately I'm getting the following error when loading the page -
TypeError: $(...).amsifySuggestags is not a function
at fetch.then.then.data (upload.php:1550)
This is the beginning of jquery.amsify.suggestags.js (https://github.com/amsify42/jquery.amsify.suggestags) -
(function($) {
$.fn.amsifySuggestags = function(options, method) {
/**
* Merging default settings with custom
* @type {object}
*/
var settings = $.extend({
type : 'bootstrap',
tagLimit : 5,
suggestions : [],
classes : [],
backgrounds : [],
colors : [],
whiteList : false,
afterAdd : {},
afterRemove : {},
}, options);
/**
* Initialization begins from here
* @type {Object}
*/
var AmsifySuggestags = function() {
this.selector = null;
this.name = null;
...
I don't get the error message when inserting the $('#inputTags').amsifySuggestags outside of the fetch method with some hardcoded values.
Thanks in advance!
来源:https://stackoverflow.com/questions/52818048/javascript-typeerror-functionname-is-not-a-function