简体   繁体   English

Flask & Js - 无法通过 post 请求获取 json

[英]Flask & Js - Can't get json through a post request

I am trying to make a site, it gets data from a flask webapp.我正在尝试创建一个网站,它从 flask 网络应用程序获取数据。
Can someone tell a way to fix this....有人可以告诉解决这个问题的方法....

Note -笔记 -
https://flask-webapp.co & https://my-site.co are examples, not real URLs... https://flask-webapp.co & https://my-site.co是示例,不是真实的 URL...
importing flask as server导入flask作为server

But when i post the data to flask, it gives me this error -但是当我将数据发布到 flask 时,它给了我这个错误 -

[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 - 我的 python 代码 -
 @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 - Js代码-
XHR failed loading: POST "https://flask-webapp.co/getData".

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".

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

NOTE -笔记 -
importing flask as server导入flask作为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 -在 js 代码中 -

let data = JSON.parse(this.responseText)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM