0

I come from a Python programming background and had to do some Javascript stuff for codebehind(using VB.Net). I am able to manage but many articles I see are including JQuery, NodeJs and so.. I can now write in Javascript but not NodeJS, JQuery (which many articles show). That is totally a new dimension for me.

Problem: In this, I am finding it really difficult to read a csv from a given local path in JS. In python it is very easy for me. But in JS, it seems near to impossible. I have 2 dropdowns and the values of 2nd dropdown should change based on selection in 1st dropdown. So I have used onchange of attribute for elements of 1st dropdown. Then it jumps to JS where every dropdown selection gives me a directory path. Then in JS it should read a CSV present in this path and show this CSV data as options of 2nd dropdown. The following is one of my tries:

csv_path = "C:\data\models\driver\steering\steeringOut.csv"; 
// (csv_path will be updated on every item click in 1st dropdown)
var req = new XMLHttpRequest();
req.open("GET", csv_path, false);
req.onreadystatechange = function () 
{
   if (req.readyState == 4 && req.status == 200) 
    {
       allText = req.responseText;
       allTextLines = allText.split(/\r\n|\n/);
    }
}

This always outputs onreadystatechange as null and readyState as 1 even though the csv is in the specified path. when googled, many articles that include JQuery or NodeJs. So that is not an option for me.This is very easy in Python but looks so difficult in JS. I have been struggling with this since a week and all my efforts seems useless in Javascript. Hence I reached out here incase if someone can help me.

Priya
  • 329
  • 3
  • 14
  • can I ask you why are you using an Http request to read from the local filesystem? Is JS running in the browser? – rick Nov 25 '22 at 09:02
  • You are trying to read a file directly from client side in your browser. You can't. You have to host your csv files somewhere using any backend you like. And make a http call using fetch. XMLHttpRequest is deprecated. – Han Moe Htet Nov 25 '22 at 09:03
  • @rick No. But the dropdowns are all created in browser and this csv_path comes from there. So I thought may be have to use. – Priya Nov 25 '22 at 09:14
  • @HanMoeHtet I am actually confused. Could you give me any example? – Priya Nov 25 '22 at 09:15
  • (1) The ```\``` character starts an escape sequence so your string doesn't contain what you think it does. (2) XMLHttpRequest deals in URLs not file paths (3) HTML documents are not permitted to help themselves to files on your local disk for security reasons: https://stackoverflow.com/a/10752078/19068 – Quentin Nov 25 '22 at 09:20
  • @HanMoeHtet — XHR is **not** deprecated. – Quentin Nov 25 '22 at 09:20

0 Answers0