如何在laravel中使用React fetch发送api令牌密钥



这是我的代码:如何使其成为职业选手,对于这种情况,什么是最好的Laravel API库?

likePost() {
fetch("api/like_post" , {
method: "POST",
headers: {
'Key': '11111111111111111'
}
})
}

我想你可以看看这个awsome Laravel API包:https://github.com/ejarnutowski/laravel-api-key,

您可以通过以下语法使代码变得更好:

首先为类添加新状态

this.state = {
likes: 0,
};

然后绑定函数:

this.likePost = this.likePost.bind(this);

并创建这样的最终函数:

likePost(post_id, user_id) {
fetch("api/like_post" , {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
'X-Authorization': 'KkKf87BOncnOGwzjLBG8Isd87T42D1yOJJNoUMsrjmUkqSIxXmHG4XPZ07UQM0Pi'
},
body: JSON.stringify({
"post_id": post_id,
"user_id": user_id
})
})
.then(res => res.json())
.then(likes => this.setState({likes:likes}))
.catch(error => {
// Log Error
console.log(error)
});
}

最新更新