Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda