select2不加载ajax数据:TypeError:c.ajax不是函数

时间:2018-01-24 17:48:01

标签: javascript jquery ajax jquery-select2

只需执行ajax hello-world 练习,我就无法加载<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#my-input').select2({ placeholder: "Search for something", minimumInputLength: 1, ajax: { url: "https://www.example.com/search", dataType: 'json', delay: 250, data: function(params) { return { "term": params.term, // search term "from": 0, "size": 20 }; }, processResults: function (data, params) { params.page = params.page || 1; data.hits = $.map(data.hits, function(obj) { obj.text = obj.text || obj.name; return obj; }); return { results: data.hits, }; }, cache: true } }); }); </script> 数据,而是收到错误:

  

未捕获的TypeError:c.ajax不是函数
     at Object.transport(select2.min.js:1)
     在d(select2.min.js:1)

我的代码:

{{1}}

1 个答案:

答案 0 :(得分:4)

基础问题出现在jquery-3.2.1.slim.min.js,根据some sources ajax已经从精简版jquery中移除,因此要使select2再次工作,需要包括完整jquery的版本:

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>