JavaScript TypeError $(...)。functionName不是函数

时间:2018-10-15 13:37:14

标签: javascript jquery fetch

我正在使用以下Javascript通过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);
?>

不幸的是,加载页面时出现以下错误-

TypeError: $(...).amsifySuggestags is not a function
at fetch.then.then.data (upload.php:1550)

这是 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;
...

在具有某些硬编码值的fetch方法之外插入$('#inputTags')。amsifySuggestags时,我没有收到错误消息。

谢谢!

0 个答案:

没有答案