简体   繁体   English

无法加载Select2 Ajax结果

[英]Select2 Ajax results could not be loaded

Whenever I try to search for result in my select2 ajax searchbar I receive the following message: 每当我尝试在select2 ajax搜索栏中搜索结果时,都会收到以下消息:

'The results could not be loaded' “无法加载结果”

Html : HTML

<select class="js-data-example-ajax form-control" multiple="multiple"></select>

Javascript : Javascript

$('select').each(function(idx, ele) {
            $(ele).select2({
                theme: 'bootstrap4',
                placeholder: ele.getAttribute('placeholder'),
                ajax: {
                    url: '/product/api/search',
                    dataType: 'json' },
                    type: 'GET',
            });
        });

I think that my Ajax settings are wrong could you assist? 我认为我的Ajax设置错误,您可以帮忙吗?

Your code should look like this. 您的代码应如下所示。 No GET method needed. 无需GET方法。

$('select').each(function(idx, ele) {
  $(ele).select2({
      ajax: {
        url: 'https://api.github.com/search/repositories',
        dataType: 'json'
        // Additional AJAX parameters
      }
   }
});

Also your problem might be in your source json format because select2 need own format with results . 另外,您的问题可能出在您的源json格式中,因为select2需要带有results自己的格式。

In order to accomplish this, Select2 expects a very specific data format. 为了实现此目的,Select2期望使用非常特定的数据格式。 This format consists of a JSON object containing an array of objects keyed by the results key. 此格式由一个JSON对象组成,其中包含由结果键键入的对象数组。

{
  "results": [
    {
      "id": 1,
      "text": "Option 1"
    },
    {
      "id": 2,
      "text": "Option 2"
    }
}

Also you can use processResults or transform data into certain format. 您也可以使用processResults或将数据转换为特定格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM