javascript - Python 无法向 axios 请求发送数据
问题描述
我有一个难题。当我从 Postman 发送请求时,我得到服务器状态 200 和所需的数据。使用 Axios 时,服务器状态为 500 internal server error 和日志中的错误:
2020-08-17T09:25:28.361222+00:00 app[web.1]: [2020-08-17 09:25:28,359] ERROR in app: Exception on / [POST]
2020-08-17T09:25:28.361223+00:00 app[web.1]: Traceback (most recent call last):
2020-08-17T09:25:28.361224+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
2020-08-17T09:25:28.361224+00:00 app[web.1]: response = self.full_dispatch_request()
2020-08-17T09:25:28.361224+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
2020-08-17T09:25:28.361225+00:00 app[web.1]: rv = self.handle_user_exception(e)
2020-08-17T09:25:28.361225+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
2020-08-17T09:25:28.361226+00:00 app[web.1]: return cors_after_request(app.make_response(f(*args, **kwargs)))
2020-08-17T09:25:28.361226+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
2020-08-17T09:25:28.361226+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2020-08-17T09:25:28.361227+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2020-08-17T09:25:28.361228+00:00 app[web.1]: raise value
2020-08-17T09:25:28.361228+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
2020-08-17T09:25:28.361229+00:00 app[web.1]: rv = self.dispatch_request()
2020-08-17T09:25:28.361229+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
2020-08-17T09:25:28.361230+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2020-08-17T09:25:28.361230+00:00 app[web.1]: File "/app/app.py", line 14, in index
2020-08-17T09:25:28.361230+00:00 app[web.1]: result = scrapper_amaz.main_scrapper(amazon_url)
2020-08-17T09:25:28.361231+00:00 app[web.1]: File "/app/scrapper_amaz.py", line 460, in main_scrapper
2020-08-17T09:25:28.361231+00:00 app[web.1]: flagged_data = save_to_mongo(data)
2020-08-17T09:25:28.361232+00:00 app[web.1]: File "/app/scrapper_amaz.py", line 440, in save_to_mongo
2020-08-17T09:25:28.361232+00:00 app[web.1]: data = collection.find_one({'url':document.get('url')})
2020-08-17T09:25:28.361304+00:00 app[web.1]: AttributeError: 'NoneType' object has no attribute 'get'
这是我的 axios 请求:
const form = new FormData()
form.append('url', `https://amazon.com/dp/${asin}`)
axios({
"method": "POST",
"url": url,
"data": form,
"headers": {
"Content-Type": "multipart/form-data"
}
}).then((response) => {
console.log(response)
}).catch((error) => {
console.log(error)
})
这是我的python app.py:
from flask import Flask, render_template, request, json # import flask
from flask_cors import CORS
import scrapper_amaz
app = Flask(__name__) # create an app instance
CORS(app)
@app.route("/", methods = ['GET', 'POST']) # at the end point /
def index(): # call method hello
if request.method == 'POST':
amazon_url = request.form['url']
result = scrapper_amaz.main_scrapper(amazon_url)
if '_id' in result:
del result['_id']
result = json.dumps(result)
result = result.replace('"[','[').replace(']"',']')
return json.dumps(json.loads(result), indent=2)
if request.method == 'GET':
return render_template('index.html') # which returns "hello world"
if __name__ == "__main__": # on running python app.py
app.run(debug=True,host='0.0.0.0', port=5000) # run the flask app
可能是什么问题呢?
解决方案
推荐阅读
- php - 以编程方式为 WooCommerce 订阅中的购物车项目设置免费试用
- mysql - 在具有自连接关系的 mysql 表中选择
- time-complexity - 循环中带有函数*函数的函数的时间复杂度
- java - 纠正有界类类型可迭代和有界通配符列表字段迭代器类型
- spring-data-mongodb - 使用 Spring Data MongoDb v2.x 时如何访问 explain() 方法和 executionStats?
- python - 在实例列表上应用 python 类方法
- android - 在我的自定义视图中找不到自定义属性
- c++ - 在将对象传递给函数期间删除复制/移动构造函数和赋值运算符后未出现编译时错误
- javascript - 从 Firebase 中检索网站的嵌套数据
- python - Python - 传递参数时类调用不起作用