Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
Why does making a GET request to URL using fetch gives me an error while injecting a script tag programmatically with URL as src doesn't?

If I inject a script programmatically like this:

((global, url) => {
    let s = document.createElement('script')
    s.src = url 
    global.document.body.appendChild(s)
})(window, 'https://example.com')

The request will go through just fine and the script will be inserted with the response to the request to "https://example.com" ... but if I try to make an explicit fetch request to the same URL I get a CORS error? Why is that happening?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You just invented JSONP :D Description from w3school:

JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.

Here is explanation on stackoverflow

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!