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

126
Vistas
Flask & Js - Can't get json through a post request

I am trying to make a site, it gets data from a flask webapp.
Can someone tell a way to fix this ....

Note -
https://flask-webapp.co & https://my-site.co are examples, not real URLs ...
importing flask as server

But when i post the data to flask, it gives me this error -

[2022-02-27 09:05:35,348] ERROR in app: Exception on /getData [POST]
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/runner/Discord-Smart-Bot/main.py", line 463, in getuser
    user = json.loads(server.request.get_json())
  File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
172.18.0.1 - - [27/Feb/2022 09:05:35] "POST /getData HTTP/1.1" 500 -
[2022-02-27 09:05:35,354] INFO in _internal: 172.18.0.1 - - [27/Feb/2022 09:05:35] "POST /getData HTTP/1.1" 500 -

My python code -
@app.route("/getData", methods=["POST"])
def getdata():
  print("GOT")
  data_rec = json.loads(server.request.get_json())
  print(user)
  id = data_res["id"]
  data = asyncio.run(getUser(int(id)))
  return data

Js code -
 xhr.open("POST", "https://flask-webapp.co/getData", true);
      xhr.getResponseHeader('Content-type', 'application/json')
      xhr.onload = function() {
        console.log(this.responseText)
        let data = JSON.parse(this.responseText)
        let val = data["val"]
        let i = data["i"]
      }
      params = {"id" : usr_id}
      xhr.send(params);

Error in browser -
Access to XMLHttpRequest at 'https://flask-webapp.co/getData' from origin 'https://my-site.co' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
POST https://flask-webapp.co/getData net::ERR_FAILED 500
XHR failed loading: POST "https://flask-webapp.co/getData".
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I fixed No 'Access-Control-Allow-Origin' header is present on the requested resource.

NOTE -
importing flask as server

from flask_cors import CORS, cross_origin

app = server.Flask(" ")
CORS(app, support_credentials=True)

@app.route("/getData", methods=["POST"])
@cross_origin(support_credentials=True)
def getuser():
  print("GOT")
  print(str(server.request.data))
  data_rec = json.loads(server.request.data)
  id = data_rec["id"]
  data = getData(int(id))
  print(data)
  return str(data)

And in js code -

let data = JSON.parse(this.responseText)
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