0

Im developing a tool in javascript that will get information from a embedded HTTP server on an external controller and display it onto a web page, i'm new to HTTP requests and have successfully followed tutorials but now i'm lost as to what to do. i have used google dev tools to monitor the traffic and i'm trying to replicate how the information is requested. The issue is i cannot seem to get the same response, i can get chrome to request the file from the controller (csv file) but when i try to use the same request headers i hit the CORS issue that i have seen on a few threads but not sure what the difference between chrome requesting the info and the script requesting the info as i understand its the server side that blocks it?

this is the header when chrome requests the file and receives it successfully

General
Request URL: http://172.20.25.11/getvar.csv
Request Method: GET
Status Code: 200 OK
Remote Address: 172.20.25.11:80
Referrer Policy: no-referrer-when-downgrade

Response Header
Cache-Control: no-cache
Content-Type: text/csv; charset="utf-8"
Expires: 1 JAN 2013 00:00:00 GMT
Last-Modified: 27 AUG 2020 15:40:07 GMT
Server: embedded HTTPD
Transfer-Encoding: chunked

Request Header
GET /getvar.csv HTTP/1.1
Host: 172.20.25.11
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
If-Modified-Since: 27 AUG 2020 15:32:21 GMT

is there any way i can write a XMLHttpRequest() or fetch() request where i can retrieve and parse this file?

Thanks in advance

JDuffy93
  • 9
  • 2
  • 1
    *its the server side that blocks it*. It is not, is the browser blocking it because the domain that you request the file from is not the same as the domain you send the request to and the response doesn't contain an `Access-Control-Allow-Origin` header with a value that permits your domain access. – Titus Aug 27 '20 at 15:49
  • Thanks you, how do i determine what header i need for the access control? is there a way of finding this from what the browser sends? – JDuffy93 Aug 28 '20 at 07:01
  • The required headers have to be set by the server. The value of the `Access-Control-Allow-Origin` needs to be set to your domain (eg: `Access-Control-Allow-Origin: https://yourdomain.com`) or to `*` if you want to allow all domains access (eg: `Access-Control-Allow-Origin: *`). You can learn more about that from [HERE](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) – Titus Aug 28 '20 at 07:06
  • Thank you for the quick replies! I think that's where my issue lies, the controller is 3rd part and i don't have access to change how the server operates. The file is available as i can download it direct from the URL but as you can tell i'm not clued up enough to find the correct method to resolve this. Currently looking at the pre-flight guides but its all related to server side as you say. – JDuffy93 Aug 28 '20 at 07:18
  • If you cannot change the server response, you will not be able to access the file from a browser. An alternative will be to create a server that downloads the file from the controller and serves it to a web app. – Titus Aug 28 '20 at 10:32
  • Thank you Titus looks like i have more research and tutorials to watch on how to do that Really appreciate your help – JDuffy93 Aug 28 '20 at 11:08

0 Answers0