Ajax request using jsonp not going through

Viewed 27

I am trying to retrieve a text file from https://www.ndbc.noaa.gov/data/realtime2/51201.txt. I am making an app to retrieve the swell height and period from the NDBC. My code below bypasses the CORS policy by using 'jsonp' as the datatype. Now I can send the request through, however, I run into an error I have not been able to find a solution to. Here is my code:


export default class GetData {
    static #url = "https://www.ndbc.noaa.gov/data/realtime2/51201.txt";

    static retrieve = (swell, period) => {
        $.ajax({
            type: 'GET',
            url: this.#url,
            dataType: 'jsonp',
            success: [ function (data) {
                console.log(data);
            }],
            error: function () {
                console.log("error");
            },
        });
    }
}

Here are the first 5 lines from the file it retrieved where the error is coming from

#YY  MM DD hh mm WDIR WSPD GST  WVHT   DPD   APD MWD   PRES  ATMP  WTMP  DEWP  VIS PTDY  TIDE
#yr  mo dy hr mn degT m/s  m/s     m   sec   sec degT   hPa  degC  degC  degC  nmi  hPa    ft
2021 07 16 02 57  MM   MM   MM   1.5     9   5.5  40     MM    MM  26.4    MM   MM   MM    MM
2021 07 16 02 27  MM   MM   MM   1.4     9   5.5  41     MM    MM  26.4    MM   MM   MM    MM
2021 07 16 01 57  MM   MM   MM   1.5     9   5.4  39     MM    MM  26.4    MM   MM   MM    MM   

Here is the error displayed through the console

Uncaught SyntaxError: Private field '#YY' must be declared in an enclosing class

The request fails because for some reason there is a javascript error, thinking that the #YY portion of the text file is a private js variable. I can't figure out why this may be happening, but it's preventing me from retrieving the file. I could be wrong assuming that that 'jsonp' is causing the js errors. If I try to revert the datatype from 'jsonp' back to 'text', I get the same old CORS/403 errors that lots of other people were familiar with. If anyone knows how I could get around this without running into the CORS/403 error please let me know. I am using react to display the data if that changes anything. A simple request to get a text file, which is allowed by the NDBC, is proving to be very difficult so any help is appreciated!

0 Answers
Related