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

178
Views
Can access api in python but cannot replicate using JavaScript

Using the below to access an api using python and it gives me a successful response. Trying to do the same thing from an html file with JavaScript on my hard drive I get the below error. Reading around the issue seems to be that I am running this from a local file not hosted on a proper server.

Access to fetch at 'https://api.connect.example.com/test/v1/state-pop/levels/rating?' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

import requests
import json
from requests.auth import HTTPBasicAuth

headers = { 'Accept': 'application/json' }
auth = ('user','password')
response_API = requests.get('https://api.connect.example.com/test/v1/state-pop/levels/rating?', auth = auth, headers=headers)
<!doctype html>
<html>
    <head>
<script>
let username = 'username';
let password = 'password';

fetch('https://api.connect.example.com/test/v1/state-pop/levels/rating?', {
  method: 'get',
  headers: {
    "Content-Type": "text/plain",
    'Authorization': 'Basic ' + btoa(username + ":" + password)
  }
})
</script>
</head>
</html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use this simple extension for your browser to avoid CORS issues also from your html file: https://mybrowseraddon.com/access-control-allow-origin.html

After the installation complete you need to toggle on the extension in the 'problematic' site:

Toggle on CORS extension after installation

(in the bottom of the picture you can see the toggle button 'ON' - just click it :))

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!