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

124
Views
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 answers
Answer question

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 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!