我正在嘗試向python後端休息發出JavaScript請求,並且出現無法解析的錯誤。我試圖測試一個listname是否在我通過axios數據傳遞的request.json對象中,但python無法看到它或引發錯誤。使用請求無法讀取python休息中的axios數據
這裏是我的Python代碼:
@app.route('/', methods=['GET', 'DELETE', 'POST'])
def profits():
if request.method=='GET':
if 'listname' in request.json():
print 'inside listname == True!!!'
conn = psycopg2.connect(database = "profitnloss3", user = "patientplatypus", password = "*************")
cur = conn.cursor()
sql = 'SELECT * FROM ledger WHERE listname = %s'
params = (request.json['listname'])
cur.execute(sql, params)
conn.commit()
data = cur.fetchall()
conn.close()
return jsonify(data)
else:
print 'inside listname == False!!!'
conn = psycopg2.connect(database = "profitnloss3", user = "patientplatypus", password = "*******************")
cur = conn.cursor()
sql = 'SELECT * FROM ledger'
cur.execute(sql)
conn.commit()
data = cur.fetchall()
conn.close()
return jsonify(data)
那是給我找麻煩相關線上if 'listname' in request.json():
這裏是我在前端想提出的Axios公司電話:
axios({
method: 'get',
url: 'http://localhost:5000/',
headers: {
'Content-type': 'application/json'
},
data:{
'listname': this.ledgername
}
})
.then((response)=>{
console.log('this is the response from the python server on a get request of listname ', response);
})
.catch(error=>{
console.log('here is the error on a get request from the python server of listname ', error);
})
我得到的錯誤是:
TypeError: 'NoneType' object is not callable
如果我改用if 'listname' in request.json:
TypeError: argument of type 'NoneType' is not iterable
我已經使用表單變量也試過(即的Request.Form),但值則完全從Axios公司忽略甚至張貼雖然不是拋出一個錯誤(我猜它不會將其視爲表單數據)。
我不知道從哪裏去,這裏的任何幫助將不勝感激。
編輯:
在我的Python文件的頂部我的導入頭是
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS, cross_origin
import sqlite3
import psycopg2
import os
from os.path import join, dirname
from dotenv import load_dotenv
from models.shared import db
from models.profits import Profit