前端与Python后台对接失败

语言:python,js
问题:前后端对接

前端发送POST

img

Postman发送Post

img

前端代码

img

前端发送Post后台响应

img

我在/getResponse也写了一个get请求的返回值,将代码改作get可以正确得到返回值

  • 关于该问题,我找了一篇非常好的博客,你可以看看是否有帮助,链接:python实现简单的post和get网络请求
  • 除此之外, 这篇博客: Python自动化接口测试框架——封装Get与Post请求方法(一)中的 封装get和post请求 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 代码片.

    # -- coding: utf-8 --
    import requests
    import json
    
    
    class Webrequests():
        def get(self,url,data,headers):     
            try:
                r = requests.get(url,params=data,headers=headers)
                r.encoding = 'utf-8'   
                json_r = r.json()
                print("Test执行结果:",json_r)
                return json_r
            except BaseException as e:
                print("请求失败!",str(e))
        def post(self,url,data,headers):    
            try:
                r = requests.post(url,data=data,headers=headers)
                r.encoding = 'utf-8'
                json_r = r.json()
                print("Test执行结果:",json_r)
                return json_r
            except BaseException as e:
                print("请求失败!",str(e))
        def post_json(self,url,data,headers):   
            try:
                data = json.dumps(data)  
                r = requests.post(url,data=data,headers=headers)
                r.encoding = 'utf-8'
                json_r = r.json()
                print("Test执行结果:",json_r)
                return json_r
            except BaseException as e:
                print("请求失败!",str(e))
    
        def run_main(self, method,url,data,headers):
            result = None
            if method == 'post':
                result = self.post(url,data,headers)
            elif method == 'get':
                result = self.get(url,data,headers)
            elif method == 'post_json':
                result = self.post_json(url, data, headers)
            else:
                print("method值错误!!!")
            return result
    

    外界调用的时候,直接调取run_main方法,传相关参数

    第一章先写到这里吧,后期逐渐更新,给大家看下运行的run类,以及生成的报告