首页 文章

使用Axios如何在GET请求中发送用户名

提问于
浏览
1

React-Django应用程序 . 用户通过Axios PUT 提供用户名和密码登录,如果返回正确的JWT和用户名,则两者都存储在 sessionStorage 中 . 然后,用户将自动路由到 /home ,其中应填充特定于用户的信息 .

/homecomponentWillMount() 应该通过Axios GET 来自数据库 /home 的内容 . 一些是静态的,一些是与用户相关的,例如 first_name . 我正在尝试将用户名与JWT一起发送以检索此信息但不确定如何操作 .

这就是我正在检索静态内容,如欢迎消息 . 只是想发送 username 所以我可以添加逻辑服务器端来检索该用户的信息并发送回响应 .

import axios from 'axios';
import { push } from 'react-router-redux';
import { ROOT_URL } from '../../config/config.json';

// Establish the different types
export const WELCOME_MESSAGE = 'welcome_message';

export function getWelcome() {

    return function(dispatch) {
        axios
            .get(
                `${ROOT_URL}/api/home/`,
                { headers: 
                    { 
                        'Content-Type': 'application/json',
                        'Authorization': 'JWT ' +  sessionStorage.getItem('token')
                    }
                }
            )
            .then(response => {
                dispatch({ 
                    type: WELCOME_MESSAGE,
                    payload: response.data
                });
            })
            .catch(error => {
                console.log("Broke");
            });
    }
}

最糟糕的情况是,我只是用 POST 创建另一个函数,我知道我可以轻松地发送这些信息 .

1 回答

  • 1

    所以你想在_29733112_中的 GET 请求方法中发送用户名
    您可以将URL中的用户名作为 query 发送,以便在后端可以从URL查询中访问用户名
    example: $/api/home?username=rahulrana 这是通过GET方法实现这一目标的方法 .
    通过URL中的查询是发送信息的唯一方法 .

相关问题