简体   繁体   English

无法使用请求在python rest中读取axios数据

[英]Cannot read axios data in python rest using request

I am trying to make a javascript request to a python backend rest, and getting errors I cannot resolve. 我试图向python后端休息发出一个javascript请求,并得到我无法解决的错误。 I am trying to test if a listname is in a request.json object that I am passing through the axios data, but python either can't see it or throws an error. 我试图测试listname是否在我通过axios数据的request.json对象中,但是python要么看不到它,要么抛出错误。

Here is my python code: 这是我的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)

The relevant line that is giving me trouble is if 'listname' in request.json(): 给我带来麻烦的相关行是if 'listname' in request.json():

Here is the axios call that I am making in the front end: 这是我在前端制作的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);
    })

The error that I am getting is: 我得到的错误是:

TypeError: 'NoneType' object is not callable

If I instead use if 'listname' in request.json: 如果我if 'listname' in request.json:使用if 'listname' in request.json:

TypeError: argument of type 'NoneType' is not iterable

I have also tried using the form variable (ie request.form) but the values are then completely ignored from the axios post even though an error is not thrown (I guess it doesn't see it as form data). 我也尝试使用表单变量(即request.form),但是即使没有抛出错误(我猜它不会将其视为表单数据),也会从axios post中完全忽略这些值。

I do not know where to go from here, any help would be appreciated. 我不知道从哪里开始,任何帮助将不胜感激。

EDIT: 编辑:

My import header at the top of my python file is 我的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

I guess you are not sending the data at all because according to the axios documentation the data option is „Only applicable for request methods 'PUT', 'POST', and 'PATCH'“ . 我想你根本就没有发送数据,因为根据axios文档data选项是“仅适用于请求方法'PUT','POST'和'PATCH'”

So you have to use a POST request (and change the server code accordingly): 所以你必须使用POST请求(并相应地更改服务器代码):

axios({
  method: 'post',
  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 post request of listname ', response);
})
.catch((error) => {
  console.log('here is the error on a post request from the python server of listname ', error);
});

or sending the listname as GET parameter instead of as JSON in the request body. 或者在请求正文中将listname作为GET参数而不是JSON发送。

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

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