You can refer to the following Stack Overflow answer if you want to set the initial value for AJAX requests: <ahref="http://stackoverflow.com/q/30316586/359284#30328989">Select2 4.0.0 initial value with AJAX</a>
The data returned by the data provider or by <code>processResults</code> should be a JSON object containing an array of objects keyed by the <code>results</code> key.
Each object should contain, <em>at a minimum</em>, an <code>id</code> and a <code>text</code> property. The text property will be displayed by default, unless you are using <code>templateResult</code> and <code>templateSelection</code> to customize the way options and selections are rendered.
</p>
<p>
The response object may also contain <ahref="examples.html#data-ajax">pagination data</a>, if you would like to use the "infinite scroll" feature. See <ahref="#what-properties-are-required-on-the-objects-passed-in-to-the-ar">"What properties are required on the objects passed in to the array?"</a> for more information. A complete example is as follows:
When options are to be generated in <code><optgroup></code> sections, options should be nested under the <code>children</code> attribute of each group object:
You can use the <code>ajax.processResults</code> option to modify the data returned from the server before passing it to Select2. The data structure returned by <code>processResults</code> should match the format as specified above.
By default, Select2 will trigger a new AJAX request whenever the user changes their search term. You can set a time limit for debouncing requests using the <code>ajax.delay</code> option.
How do I tell Select2 which URL to get the results from?
</h3>
<p>
When connecting Select2 to a remote data source, you have the option of using either a single endpoint (a single page which handles all requests) or a dynamic endpoint (one of many pages). You can point Select2 to a single endpoint during initialization by specifying a string for the <code>ajax.url</code> option.
</p>
{% highlight js linenos %}
$('select').select2({
ajax: {
url: '/path/to/search/endpoint'
}
});
{% endhighlight %}
<p>
If there isn't a single url for your search results, or you need to call a function to determine the url to use, you can specify a function for the <code>ajax.url</code> option, and this will be used instead. The query parameters will be passed in through the <code>params</code> option.
By default, Select2 will send the query term as well as the pagination data as query parameters in requests. You can override the data that is sent to your API, or change any of the query parameters, by overriding the <code>ajax.data</codE> option.
Select2 expects that the results that are returned from the remote endpoint are already filtered ahead of time based on the search term. If your remote endpoint just returns the list of all possible options, you may be interested in using Select2's <ahref="examples.html#data-array">support for data arrays</a>.
Select2 uses the transport method defined in <code>ajax.transport</code> to send requests to your API. By default, this transport method is <code>jQuery.ajax</code> but this can be changed.