簡體   English   中英

無法使用請求在python rest中讀取axios數據

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

我試圖向python后端休息發出一個javascript請求,並得到我無法解決的錯誤。 我試圖測試listname是否在我通過axios數據的request.json對象中,但是python要么看不到它,要么拋出錯誤。

這是我的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:使用if 'listname' in request.json:

TypeError: argument of type 'NoneType' is not iterable

我也嘗試使用表單變量(即request.form),但是即使沒有拋出錯誤(我猜它不會將其視為表單數據),也會從axios post中完全忽略這些值。

我不知道從哪里開始,任何幫助將不勝感激。

編輯:

我的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

我想你根本就沒有發送數據,因為根據axios文檔data選項是“僅適用於請求方法'PUT','POST'和'PATCH'”

所以你必須使用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);
});

或者在請求正文中將listname作為GET參數而不是JSON發送。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM