【问题标题】:Angular get request with param to Flask REST resource not working带有参数的角度获取请求到 Flask REST 资源不起作用
【发布时间】:2021-09-19 21:37:17
【问题描述】:

我正在从 Angular 向 Flask REST 资源(没有查询参数)发出获取请求,并且一切正常(收到请求并在视图中显示数据)。 问题是当我尝试将一些参数发送到 Flask 资源(使用 get)时,我从 Flask 收到'TypeError get() 缺少 1 个必需的位置参数:'startdate'。 请参阅以下我的源代码摘录:

角度(canvases.service.ts)

export class CanvasesService {
   
  constructor(private http:HttpClient) {}

  getDeliveredLastMonth(startdate: string):Observable<any> {
    
    startdate=startdate.trim();
     
    // Add safe, URL encoded search parameter if there is a search term
    const options = startdate ?
     { params: new HttpParams().set('StartDate', startdate) } : {};
    
    return this.http.get("http://127.0.0.1:5000/deliveredlastmonth", options)

  }

烧瓶

class DeliveredLastMonth(Resource):
     
    def get(self,startdate):
        con = sqlite3.connect (r"C:\Users\Documents\Python\jiradata.db")
        cur=con.cursor()
        #Delivered from start date onwards 
        query="SELECT canvid,product,summary,status,resolveddate from CAnalysis where releasedate>='" +startdate+ "')"

        resultset=cur.execute(query).fetchall()
        data=pd.DataFrame(resultset)
        field_names = [i[0] for i in cur.description]
        data.columns=field_names

        return Response(data.to_json(orient="records"), mimetype="application/json")

.....
.....

api.add_resource(DeliveredLastMonth, '/deliveredlastmonth')  

您能否帮我解决为什么在向 get 请求中添加参数时这不起作用?同样,它在没有参数的情况下工作(从静态查询中获取所有数据),但是在花了很多时间浏览了几个教程之后,我在考虑参数(进行动态数据库查询)时努力让它工作。 非常感谢!

【问题讨论】:

    标签: angular flask-restful


    【解决方案1】:

    试试这个

    class DeliveredLastMonth(Resource):
        # remove startdate from here
        def get(self):
            # get startdate from query string
            startdate = request.args.get("startdate")
    
            con = sqlite3.connect (r"C:\Users\Documents\Python\jiradata.db")
            cur=con.cursor()
            #Delivered from start date onwards 
            query="SELECT canvid,product,summary,status,resolveddate from CAnalysis where releasedate>='" +startdate+ "')"
    
            resultset=cur.execute(query).fetchall()
            data=pd.DataFrame(resultset)
            field_names = [i[0] for i in cur.description]
            data.columns=field_names
    
            return Response(data.to_json(orient="records"), mimetype="application/json")
    
    
    api.add_resource(DeliveredLastMonth, '/deliveredlastmonth') 
    

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 2021-02-26
      • 2015-06-06
      • 2019-04-10
      • 1970-01-01
      • 2019-02-07
      • 2019-12-01
      • 1970-01-01
      • 2019-06-05
      相关资源
      最近更新 更多