react本机jwt-auth错误获取API
我试图通过使用JWT-AUTH,以下方法来获取用户详细信息。
login() {
alert(this.state.UserName);
fetch (Api+''+'/wp-json/jwt-auth/v1/token',{
method: "POST",
username: this.state.UserName,
password: this.state.Password
}).then((response) => response.json())
.then((responseData) =>{
console.log("LoginData:-" + JSON.stringify(responseData));
}).done();
}
}
并获得错误: -
{
"code":"[jwt_auth] empty_username",
"message":"<strong>ERROR</strong>: The username field is empty.",
"data":{"status":403}
}
如果有人知道,请提供帮助。
我不再认为您需要答案,因为它已经发布了一年,但是否有人需要
如下所示,如下所示,您需要将数据传递到hody 和 jwt token中的标题之类并更新如果有任何问题
login(){
fetch(Api+''+'/wp-json/jwt-auth/v1/token', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'JWT '+<token> // There will be different type of authorization like Bearer, JWT, Basic
},
body: JSON.stringify({
username: this.state.UserName,
password: this.state.Password
}),
})
.then((response) => response.json())
.then((responseData) => {
console.log("LoginData:-" + JSON.stringify(responseData));
}).done();
}
如果您的API使用基本身份验证,则需要添加用户名和密码作为身份验证标头
示例
const base64 = require('base-64');
login() {
alert(this.state.UserName);
fetch (Api+''+'/wp-json/jwt-auth/v1/token',{
method: "POST",
headers: {
'Authorization', 'Basic ' + base64.encode(this.state.UserName+ ":" + this.state.Password)
}
}).then((response) => response.json())
.then((responseData) =>{
console.log("LoginData:-" + JSON.stringify(responseData));
}).done();
}
}