Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with AJAX, JS, and Select2
    primarykey
    data
    text
    <p>I apologize if I am doing something stupid, but I am brand new to Select2 and Ajax and am having a hell of a time trying to get this work. I am attempting to create my own json file (<s>link removed</s>) to use with a form (<s>link removed</s>) I am building. Using Firefox Lite in Chrome, I am able to get a response from my server (200 OK), but the select box does not display the search results. My json file appears to validate correctly using <a href="http://jsonformatter.curiousconcept.com" rel="nofollow">http://jsonformatter.curiousconcept.com</a>. You can also see it at </p> <p>Any thoughts or tips?</p> <p><strong>json.php source:</strong></p> <pre><code>&lt;? $myArray = array( array( "id" =&gt; "id1", "text" =&gt; "title1" ), array( "id" =&gt; "id2", "text" =&gt; "title2" ) ); echo json_encode($myArray); ?&gt; </code></pre> <p><strong>select.php source:</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;link href="select2/select2.css" rel="stylesheet"/&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="select2/select2.js"&gt;&lt;/script&gt; &lt;script&gt; function movieFormatResult(movie) { var markup = "&lt;table class='movie-result'&gt;&lt;tr&gt;"; markup += "&lt;td class='movie-info'&gt;&lt;div class='movie-title'&gt;" + movie.title + "&lt;/div&gt;"; markup += "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;" return markup; } function movieFormatSelection(movie) { return movie.title; } &lt;/script&gt; &lt;script id="script_e6"&gt; $(document).ready(function() { $("#e6").select2({ placeholder: "Search for a movie", minimumInputLength: 1, ajax: { // instead of writing the function to execute the request we use Select2's convenient helper url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json", dataType: 'jsonp', data: function (term, page) { return { q: term, // search term page_limit: 10, apikey: "my-api-key" // please do not use so this example keeps working }; }, results: function (data, page) { // parse the results into the format expected by Select2. // since we are using custom formatting functions we do not need to alter remote JSON data return {results: data.movies}; } }, formatResult: movieFormatResult, // omitted for brevity, see the source of this page formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller }); }); &lt;/script&gt; &lt;script id="script_e6a"&gt; $(document).ready(function() { $("#e6a").select2({ placeholder: "Search for a movie", minimumInputLength: 1, ajax: { // instead of writing the function to execute the request we use Select2's convenient helper url: "http://willwelch.net/play/nhs/pstudents/json.php", dataType: 'jsonp', data: function () { return; }, results: function () { // parse the results into the format expected by Select2. return {results: data}; } }, formatResult: movieFormatResult, // omitted for brevity, see the source of this page formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page dropdownCssClass: "bigdrop" // apply css that makes the dropdown taller }); }); &lt;/script&gt; &lt;/head&gt; &lt;body style="height:500px;"&gt; &lt;p&gt;original example (works)&lt;/p&gt; &lt;input type="hidden" class="bigdrop" id="e6" style="width: 600px; display: none;"&gt;&lt;br&gt;&lt;br&gt; &lt;p&gt;my version (does not work)&lt;/p&gt; &lt;input type="hidden" class="bigdrop" id="e6a" style="width: 600px; display: none;"&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload