Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 转换响应数据select2不工作_Javascript_Jquery_Jquery Select2 - Fatal编程技术网

Javascript 转换响应数据select2不工作

Javascript 转换响应数据select2不工作,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,我使用select2(v4)并使用远程数据。响应正确,但processResults函数不调用,选择2不显示任何内容 $('#country').select2({ placeholder: 'Select a country', minimumInputLength: 3, ajax: { url: 'https://battuta.medunes.net/api/country/search/?key=xx', dataTy

我使用select2(v4)并使用远程数据。响应正确,但processResults函数不调用,选择2不显示任何内容

 $('#country').select2({
     placeholder: 'Select a country',
     minimumInputLength: 3,
     ajax: {
         url: 'https://battuta.medunes.net/api/country/search/?key=xx',
         dataType: 'json',                        
         processResults: function(data) {
            var results = [];
            $.each(data, function (index, country) {
                 results.push({
                     id: country.code,
                     text: country.name
                 });
             });

             return {
                results: results
             };                            
         },                        
         data: function(params) {
            var query = {
               country: params.term
            }

            return query;
        }
     },
     width: 'resolve',
  }); 
来自ajax请求的响应示例:

[
  {"name": "Indonesia", "code": "Id"}, 
  {"name": "French Polynesia", "code": "pf"}
]

下面是它的选择代码

$('#country').select2({
     placeholder: 'Select a country',
     minimumInputLength: 3,
     ajax: {
         url: function(param){return 'https://battuta.medunes.net/api/country/search/'},
         dataType: 'jsonp' ,
         data: function (params) {


      var query = {
       country: params.term,
      // callback :"?",
       key:"00000000000000000000000000000000" //put your key here 
      }
//this is important to make sure no extra params are added becuase the api rejects anything that has wrong params
      // Query parameters will be ?city=[term]&callback=?,key=
      return query;
    },
          processResults: function(data) {
            var results = []; 
            $.each(data, function (index, country) {
                 results.push({
                     id: country.code,
                     text: country.name
                 });
             });

             return {
              "results":results
             };                            
         },

     },

     width: 'resolve',
  }); 

这是我展示工作国搜索的小提琴

谢谢@shyam joshi,这是我的作品。问题是
dataType:json
,应该是
dataType:jsonp