前后端联调

  1. CORS跨域

    #允许所有源来跨域
    CORS_ORIGIN_ALLOW_ALL =True

    #或者设置跨域请求白名单
    # CORS_ORIGIN_WHITELIST = (
    #     'http://127.0.0.1:8080',
    #     'http://localhost:8080',
    # )

    #允许携带cookie
    CORS_ALLOW_CREDENTALS = True
    1234567891011

    前后端联调思路

    1. 写完视图函数,使用postman进行接口测试,保证后端接口没有问题

    2. 在vue中写页面,向后端发送数据

    后端模板

    def login(request):

       body_dict = json.loads(request.body)

       name = body_dict.get("name")
       pwd = body_dict.get("pwd")

       if not all([name,pwd]):
           resp = {
               'code':1001,
               'msg':'信息不全'
          }
           return JsonResponse(resp)
       if name == '天听' and pwd == '123456':
           res = {
               "code":0,
               "msg":"登录成功",
               "data":{
                   "id":1,
                   "name":"tianting",
                   "age":20
              }
          }
           return JsonResponse(res)
       return JsonResponse({
           "code":1002,
           "msg":'用户名或密码错误'
      })
posted @ 2020-09-28 21:23  CefiLing  阅读(252)  评论(0)    收藏  举报