0

I want to use the CSV file data for autosuggestion. But I can't load the data in my console.

My Code is

     var return_first = function () {
        var tmp = null;
        $.ajax({
            
             url:"state_abbrs.csv",
             dataType: "text",
             success: function (data) {
             var data=data.split(/\r?\n|\r/);
    
                tmp = data;
            }
        });
        return tmp;
    }();
    console.log(return_first);
  • Thanks, No Actually I want to see all of the CSV files data in my console – Muzibur Rahman Mar 24 '22 at 06:46
  • The point is, you can't run a site from the local file system in the browser and perform ajax requests. The error (you removed from the question) states _"Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https"_ notice how `file` isn't one of the supported schemes. – phuzi Mar 24 '22 at 06:52
  • Your code uses `$.ajax` so it's likely you're using jQuery from a browser. I fail to see how your question differs from the linked question. – phuzi Mar 24 '22 at 06:53
  • @phuzi There's nothing indicating the OP is loading from the local file system. The problem is likely that he's trying to `return tmp` before `tmp` is set. – Ruan Mendes Mar 24 '22 at 07:05
  • @MuziburRahman See https://www.dotnetcurry.com/jquery/1022/jquery-ajax-deferred-promises You should be using `$.ajax` as a promise and handling it through a then handler instead of a success property in your call. – Ruan Mendes Mar 24 '22 at 07:11
  • 1
    @JuanMendes There was when i suggested the duplicate. Check the edits and the error shown was _"Access to XMLHttpRequest at 'file:///home/rukon/Desktop/test/state_abbrs.csv' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https"_ – phuzi Mar 24 '22 at 08:42

0 Answers0