我有这个fetch()方法,它将我的react-native app中的数据发送到laravel方法
async handleSubmit(){
var me = this.state.message;
console.log('this connected',me);
try {
let response = await fetch('http://no-tld.example/androidLogin', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: 'chesterfield@gmail.com',
password: '123456',
})
});
//let res = await response.text();
if (response.status >= 200 && response.status < 300) {
console.log(response);
} else {
//Handle error
// let error = res;
// throw error;
}
} catch(error) {
//console.log(res);
}
}
我可以使用此方法接收数据
public function androidLogin(){
$rawData = file_get_contents("php://input");
$postedValue = json_decode($rawData);
//parse_str($postedValue, $output);
return response()->json([
'name' => $postedValue,
'route' => $postedValue
]);
}
并尝试返回刚发布的数据。发布的数据如下所示
下午12:35:07: {&#34;类型&#34;:&#34;默认&#34;&#34;状态&#34;:200,&#34; OK&#34;:真,&#34;头&#34 ;: {&#34;映射&#34; {&#34;连接&#34;:[&#34;保活&#34],&#34;内容长度&#34;:[&#34; 54& #34],&#34;内容类型&#34;:[&#34;应用/ JSON&#34],&#34;的Set-Cookie&#34;:[&#34; XSRF-TOKEN = eyJpdiI6IlF1NWlLOE9rVCtlUXNpYzBFSTV0c0E9PSIsInZhbHVlIjoiNWtGenprRmJOYTVsc2dQRjNrcmpxZXhWeFZRd1NZSzdiOWFKUUZTZmJJaEN6U0RnbW9uOVZ4bGUrV2ZMYUlIb0NQNHFrT1pCWXB0dnlwTjhPWm56ZWc9PSIsIm1hYyI6IjU3NDJkNWE5M2U4YmIwNTUwNzhkZTM4ZTRlNDc5OTZhNjczYWEyODU0OGNmN2ViNDdkYTM4YjdjY2U1ZWE1ZmYifQ %3D%3D; expires =周五,09-Jun-2017 11:35:07 GMT;最大年龄= 7200;路径= /, laravel_session = zqcMrXeuwwGpEsR8Jh2WakDg0cdqLod4QsfMnfcd;期满=星期五, 09-Jun-2017 11:35:07 GMT;最大年龄= 7200;路径= /; HttpOnly&#34;],&#34; access-control-allow-methods&#34;:[&#34; GET,POST,PUT,DELETE, OPTIONS&#34],&#34;存取控制允许来源&#34;:[&#34; *&#34],&#34;缓存控制&#34;:[&#34;无-cache, 私人&#34],&#34;服务器&#34;:[&#34;阿帕奇/ 2.4.18 (Ubuntu)&#34;],&#34;保持活力&#34;:[&#34;超时= 5,最大= 100&#34;],&#34;日期&#34;:[&#34 ; 6月9日星期五 2017 09:35:07 GMT&#34]}}&#34; URL&#34;:&#34; http://no-tld/androidLogin&#34;&#34; _bodyInit&#34;:&#34; {\&#34;电子邮件\&#34;:\&#34; chesterfield@gmail.com \&#34; \&#34;密码\&#34;:\&#34; 123456 \&#34;}&#34 ;,&#34; _bodyText&#34;:&#34; {\&#34;电子邮件\&#34;:\&#34; chesterfield@gmail.com \&#34; \&#34;密码\&#34;:\&#34; 123456 \&#34;}&#34;}
我现在想要从我的native-react应用程序访问返回的电子邮件。
console.log(response.email);
返回null。如何在react native中访问返回的电子邮件值?
答案 0 :(得分:0)
尝试以下提取电话,
React-native log-android // Android
或react-native log-ios // IOS
用于查看回复数据或错误详情
int
答案 1 :(得分:0)
我这样修理了
public class ServiceExample extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO: Implement this method
return null;
}
@Override
public void onCreate() {
// TODO: Implement this method
super.onCreate();
//Do Something
}
为了确保返回的帖子数据,您可以修改服务器端的发布数据并使用此功能返回
async handleSubmit(){
var me = this.state.message;
console.log('this connected',me);
try {
let response = await fetch('http://198.74.51.225/androidLogin', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: 'chesterfield@gmail.com',
password: '123456',
})
});
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
let userData = JSON.parse(res);
console.log(userData.email);
} else {
//Handle error
// let error = res;
// throw error;
}
} catch(error) {
//console.log(res);
}
}
为了实现这一目标,我还必须允许使用此中间件的人员
//Android Login
public function androidLogin(){
$rawData = file_get_contents("php://input");
$postedValue = json_decode($rawData,true);
$token = rand(400,7833);
return response()->json([
'email' => $token."_".$postedValue['email'],
'password' => $postedValue['password']
]);
}
我在我的路线中使用它
<?php
namespace App\Http\Middleware;
use Closure;
class Cors {
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
}
希望能帮助某人尝试发布或从反应原生应用中获取。