React Native/Laravel

编程入门 行业动态 更新时间:2024-10-18 16:53:16
本文介绍了React Native/Laravel-向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/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

本文发布于:2023-11-26 20:47:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:React   Native   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!