-1

I am facing an issue, where my Select2 dropdown keeps showing "Searching..." (screenshot attached)

Searching... stuck screenshot

This is my JS code:

<script>
    $(document).ready(function() {
        $("#js-data-example-ajax").select2({
            placeholder: 'Search for a stock',
            ajax: {
            url: "http://127.0.0.1:8000/stocks_list",
            contentType: 'application/json',
            dataType: 'json',
            type: "GET"
            },
        });
    });
</script>

HTML select field:

<select style="width: 300px;" id="js-data-example-ajax" name = "js-data-example-ajax" class="js-data-example-ajax"></select>

The endpoint in the AJAX call above returns a JSON object with "results" key and a list of objects for the search results (as expected by Select2), like below:

Response from local endpoint screenshot

I'm expecting to display the "text" fields as my dropdown results in the search results & I've followed the Select2 docs so far.

Barkermn01
  • 6,781
  • 33
  • 83
gakshat
  • 3
  • 3
  • Is your web server on the same hostname and port? So in this case you talking to 127.0.0.1 on port 8080 is the URL you're connecting to that is running this jQuery also on 127.0.0.1 and port 8080 otherwise you're going to run into [CORS] issues also you have not stated what the browser "console" is saying press 12 and click the console tab and look for any errors. – Barkermn01 Nov 27 '22 at 05:19
  • Thanks @Barkermn01, I initially suspected that but I have even tried external API URLs (like the ones given in the Select2 docs -> https://api.github.com/orgs/select2/repos), it did not solve the issue. My console is empty, no error messages there. – gakshat Nov 27 '22 at 05:37
  • External will still cause CORS errors, and sorry last comment F12, but you are not allowed to request resources from other sites (different schema, hostname or IP, or port) unless they send headers that authorize your origin. please have a read of https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Barkermn01 Nov 27 '22 at 05:39
  • I've added 'Access-Control-Allow-Origin' = "http://localhost/scalefin/dashboard/investments.php" in my API's header, still the same issue. – gakshat Nov 27 '22 at 06:05
  • you also need the `Access-Control-Allow-Method: GET, OPTIONS`, and these 2 are not exact but it's what i use for them `Access-Control-Expose-Headers: Origin, X-Requested-With, Content-Type` and `Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type` – Barkermn01 Nov 27 '22 at 06:08
  • I got an error on the console now; Access to XMLHttpRequest at 'http://127.0.0.1:8000/stocks_list?_type=query' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. But I've added this policy to the response header already? – gakshat Nov 27 '22 at 06:16
  • That would say you have not added the CORS headers still, have you added them to the code for the stocks_list page (I'm assuming you did), so we're going to need to see the code for the PHP file then – Barkermn01 Nov 27 '22 at 06:17
  • Yeah, I double confirmed with the Network tab in Chrome, the response header for my API shows: access-control-allow-headers: Origin, X-Requested-With, Content-Type access-control-allow-method: GET, OPTIONS access-control-allow-origin: http://localhost/ access-control-expose-headers: Origin, X-Requested-With, Content-Type – gakshat Nov 27 '22 at 06:20
  • i have added and answer that is general but to be able to fix it exactly in your code would need your PHP code adding to your question. – Barkermn01 Nov 27 '22 at 06:25

1 Answers1

0

So your error is cause by CORS,

CORS is a pain in the ass for local development, how you deal with CORS is the target of the AJAX request must return specific headers, and the easiest way to do it in PHP is just to use the below code,

This is the most permissive you can be with CORS baring support for custom headers if you need to use custom headers you need to update the Access-Control-Allow-Headers and Access-Control-Expose-Headers headers

<?php
$givenOrigin = get_header("Origin")??get_header("origin");

header("Access-Control-Allow-Origin: {$givenOrigin}");
header("Access-Control-Allow-Headers: GET, POST, PUT, PATH, DELETE, OPTIONS, HEAD"); // this allows all HTTP methods
header("Access-Control-Allow-Methods: Origin, X-Requested-With, Content-Type, Accept, localization");
header("Access-Control-Expose-Headers: Origin, X-Requested-With, Content-Type, Accept, localization");
Barkermn01
  • 6,781
  • 33
  • 83
  • I've added this to my : – gakshat Nov 27 '22 at 06:31
  • No PHP headers have to be output before any content is it must be at the very top of your file the only line of code that should be above this is if your using a namespace then the `namespace` definition goes just before this block of code. if you use `error_reporting(E_ALL)l` you will see it complaining about headers after content – Barkermn01 Nov 27 '22 at 06:33
  • I added it even above my opening, and just below it, still the same error in console. Even tried 'Access-Control-Allow-Origin: *' – gakshat Nov 27 '22 at 06:44
  • So without seeing your PHP code, and going by what you have said i don't have a clue why it's not working but we narrowed it down to it's definitely a CORS error. – Barkermn01 Nov 27 '22 at 06:46
  • This is my code till body: https://codeshare.io/VZMeQ4 – gakshat Nov 27 '22 at 06:52
  • edit your question to include it, please... – Barkermn01 Nov 27 '22 at 06:52
  • Added the link above – gakshat Nov 27 '22 at 06:55
  • oh your putting that into the wrong file, you need to put it into the file that is giving you the stocks_list not the file making the AJAX connection to it. – Barkermn01 Nov 27 '22 at 06:57