如何使用POST方法获取select2的数据

How to get data of select2 with POST method

我需要使用 POST 方法 return select2 的数据。 有谁知道我能做什么? 我试过了,但没有成功。

$("#action").select2({
  allowHtml: true,
  ajax: {
    type: "POST",
    url: URL,
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer " + localStorage.getItem("api_token")
    },
    dataType: "json",
    data: function(term, page) {
      console.log(term);
      return {
        string: term
      };
    },
    processResults: function(data) {
      console.log(data);
      return {
        results: data.map(item => {
          return {
            text: item.title,
            id: item.id
          };
        })
      };
    }
  },
  allowClear: !0
});
<input class="form-control" id="action">

代码甚至没有显示日志。

问题是我添加了输入而不是 select 标签 我这样解决问题

$("#action").select2({
  allowHtml: true,
  ajax: {
    type: "POST",
    url: URL,
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer " + localStorage.getItem("api_token")
    },
    dataType: "json",
    data: function(term, page) {
      console.log(term);
      return {
        string: term
      };
    },
    processResults: function(data) {
      console.log(data);
      return {
        results: data.map(item => {
          return {
            text: item.title,
            id: item.id
          };
        })
      };
    }
  },
  allowClear: !0
});
<select class="form-control" id="action"></select>