0

I'm working on a website which requests some data from an API. I'm requesting quite a lot of data so I'm using a template string.

The template string gets parsed correctly but inside the json it messes up. It randomly for some of the numbers adds a "." at the end of the number.

e.g. "http://logs.tf/json/2223521" => "http://logs.tf/json/2223521."

Something to note is that I'm using async: false

Here's my code:

    function myTest() {
        for (Id =2223535; Id >= 2223500; Id--) {
            console.log(Id, `http://logs.tf/json/${Id}`);//returns the proper value
            $.getJSON(`http://logs.tf/json/${Id}`, function (idData) { //sometimes adds a dot to the url
                console.log("success");
            });
        }
    }

E.g what I get from the console(for myTest()):

2223523 "http://logs.tf/json/2223523"
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://logs.tf/json/2223523. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).  (unknown)
2223522 "http://logs.tf/json/2223522"
success
2223521 "http://logs.tf/json/2223521"
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://logs.tf/json/2223521. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
TheBv
  • 150
  • 6
  • Very curious. Almost definitely a bug in your code. Can you modify myTest() adding the same getjson call as in myTestfix() and show the results here ? – Vanquished Wombat Apr 14 '19 at 10:25
  • @VanquishedWombat actually I just realized, that .replace(".","") doesn't fix the issue since it also replaces the dot in the main URL (logs.tf). My mistake – TheBv Apr 14 '19 at 12:53
  • This is of "Access-Control-Allow-Origin". Please look at https://stackoverflow.com/questions/31276220/cors-header-access-control-allow-origin-missing. – zainul ogna Apr 14 '19 at 17:24

1 Answers1

0

Okay so I think I figured it out. It had something to do with the api service I was making the Api request to if I do too many requests I get the error from above.

If I add some arbitrary calculations such as "let number = 452^2" the issue disappears since it delays the time between one request and the next one.

It also doesn't mysteriously add a "." to the request that was just there because the error which got spat out used a dot at the end of the sentence.

TheBv
  • 150
  • 6