响应本地JWT-auth错误获取api
我试图通过以下方法使用jwt获取用户详细信息。
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}
}如果有人知道请帮忙。
发布于 2018-12-12 08:30:59
我认为你不再需要答案了,因为这一年来我一直在发问这个问题,如果还有人需要的话
如Reactinative的联网选项卡所示,您需要将数据传递到headers中的body和JWT令牌,如下所示,这对我是有用的,因此,如果存在任何问题,请查看并更新
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();
}发布于 2017-09-11 14:11:16
如果您的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();
}
}https://stackoverflow.com/questions/46157487
复制相似问题