使用 Axios 如何在 GET 请求中发送用户名
Using Axios how to send username in GET request
React-Django 应用程序。用户通过 Axios PUT
提供用户名和密码登录,如果返回正确的 JWT 和用户名,两者都存储在 sessionStorage
中。然后用户将自动路由到 /home
,其中应填充特定于用户的信息。
/home
有 componentWillMount()
应该通过 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
创建另一个函数,我知道我可以轻松发送此信息。
所以你想在AXIOSGET请求方法中发送用户名。
您可以将 URL 中的用户名作为 查询 发送,这样在后端您就可以从 URL 查询
中访问用户名
示例:
${ROOT_URL}/api/home?username=rahulrana
这就是您可以通过 GET 方法实现此目的的方法。
通过URL中的查询是发送信息的唯一途径。
React-Django 应用程序。用户通过 Axios PUT
提供用户名和密码登录,如果返回正确的 JWT 和用户名,两者都存储在 sessionStorage
中。然后用户将自动路由到 /home
,其中应填充特定于用户的信息。
/home
有 componentWillMount()
应该通过 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
创建另一个函数,我知道我可以轻松发送此信息。
所以你想在AXIOSGET请求方法中发送用户名。
您可以将 URL 中的用户名作为 查询 发送,这样在后端您就可以从 URL 查询
中访问用户名
示例:
${ROOT_URL}/api/home?username=rahulrana
这就是您可以通过 GET 方法实现此目的的方法。
通过URL中的查询是发送信息的唯一途径。