2017-04-25 93 views
0

koa2 ctx.request.query没有被定义当我做一个Ajax请求,但它工作正常,当我做这个'后'在邮递员的要求。koa2 ctx.request.query没有定义,当我做一个ajax请求,但在邮递员工作正常

在邮递员,我可以得到查询对象 enter image description here

但通过爱可信 enter image description here

我的请求代码与

<template> 
    <div id="login"> 
    <mt-field label="用户名" placeholder="请输入用户名" v-model="username"></mt-field> 
    <mt-field label="密码" placeholder="请输入密码" type="password" v-model="password"></mt-field> 
    <mt-button type="primary" @click="login">primary</mt-button> 
    </div> 
</template> 

<script> 

import Axios from 'axios' 
export default { 
    name: 'login', 
    data() { 
    return { 
     username: '', 
     password: '' 
    } 
    }, 
    mounted(){ 
    console.log(Axios) 

    }, 
    methods: { 
    login(){ 
     var username = this.username; 
     var password = this.password; 
     Axios({ 
     method: 'post', 
     url: "api/user/login", 
     data: { 
      username: username, 
      password: password, 
     }, 
     }).then((res)=>{ 
     console.log(res) 
     if(res.data.code !== 200) { 
      console.log("登录失败") 
     } 
     }); 

    } 
    } 


} 
</script> 

后端:

'POST /api/user/login': async (ctx, next) => { 
     ctx.response.type = 'application/json;charset=utf-8'; 
     console.log(ctx.request.query) 
     await Db.User.login(ctx.query) 
       .then(function(res){ 
        ctx.response.body = res 
       }); 
    } 

我认为在koa2 ctx.request.query一些问题,如果只接收特定的“形式”

回答

0

在你的邮差例如,这些参数在查询字符串中传递:

POST /api/user/login?username=aa&password=aa 

这意味着您可以通过ctx.request.query访问它们。

在您的Axios示例中,数据将在请求正文中传递(POST请求更常见),这意味着您需要诸如koa-bodyparser之类的内容并通过ctx.request.body访问数据。

相关问题