React Native/Laravel-向API发送请求 [英] React Native / Laravel - Sending a request to API
问题描述
我正在实现一个使用Laravel作为与数据库通信的API的移动应用程序.
I'm implementing an mobile application that uses Laravel as an API to communicate with a database.
我想将我的本地应用程序的用户名和密码发送给Laravel.我正在使用以下代码来做到这一点:
I want to send a username and password from my react native application to Laravel. I am using the following code to do so:
fetch("mysite.com/api/login", {method: "POST", body: JSON.stringify({username: this.state.username, password: this.state.password})})
.then((response) => response.json())
.then((responseData) => {
this.handleLogin(responseData);
})
.done();
}
但是这使用JSON.stringify
来发送数据.但是如何在Laravel应用程序中处理该问题?目前,我通过以下方式接受数据:
But this uses JSON.stringify
to send across the data. But how do I handle that inside my Laravel application? Currently, I am accepting the data by:
public function login(Request $request)
{
$username = $request->get('username');
}
但是,由于发送的数据是JSON格式,因此这显然不起作用.那么在PHP中,我需要解码JSON参数吗?
However, this is obviously not working since the data being sent is in JSON format. So inside PHP, do I need to decode the JSON parameter?
推荐答案
如果您从邮递员那里获取输出,则说明获取部分存在问题,请尝试
public function login(Request $request)
{
$username = $request-> username;
}
if you are getting output from postman then there is an issue in the fetching part else try
public function login(Request $request)
{
$username = $request-> username;
}
这篇关于React Native/Laravel-向API发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!