2017-04-25 2 views
0

koa2 ctx.request.queryは定義されていませんが、郵便配達員の要求。郵便配達でkoa2 ctx.request.queryは、私がajaxリクエストを作成したときに定義されていませんが、郵便受けでうまく動作します。

私は、クエリオブジェクト enter image description here

を得ることができます。しかしaxios 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 
       }); 
    } 

私はあなたのポストマンの例では

答えて

0

それが唯一の特定の「フォーム」を受信した場合、いくつかの問題は、koa2 ctx.request.queryであると思い、パラメータは、クエリ文字列に渡されます。どの手段

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

ctx.request.queryからアクセスできます。

Axiosの例では、データはリクエスト本体で渡されます(これはPOSTリクエストでより一般的です)。つまり、koa-bodyparserのようなものが必要になり、ctx.request.bodyからデータにアクセスします。

関連する問題