尝试使用react native使用api
我用laravel创建了一个api,现在我希望能够使用React native使用它,但它根本不起作用,我对此一无所知。下面是一些代码,我认为可以帮助您尝试帮助我 这是我的api.js文件尝试使用react native使用api,api,react-native,Api,React Native,我用laravel创建了一个api,现在我希望能够使用React native使用它,但它根本不起作用,我对此一无所知。下面是一些代码,我认为可以帮助您尝试帮助我 这是我的api.js文件 import {create} from 'apisauce'; import { AsyncStorage } from 'react-native'; const api = create({ baseURL:'http://127.0.0.1:8000/api', }); api.addAsy
import {create} from 'apisauce';
import { AsyncStorage } from 'react-native';
const api = create({
baseURL:'http://127.0.0.1:8000/api',
});
api.addAsyncRequestTransform( request => async () => {
const token = await AsyncStorage.getItem('@PetFinder:token');
if(token) {
request.headers['Authorization'] = `Bearer ${token}`;
}
request.headers['Content-Type'] = `application/json`;
request.headers['accept'] = `application/json`
});
这是我的登录页面
import React from 'react';
import { View, Button } from 'react-native';
import api from '../../services/api'
export default function Login() {
async function handleSubmit()
{
const response = await api.post('/login', {
email: 'nowahmst@gmail.com',
password:'123456',
});
}
return (
<View><Button onPress={handleSubmit} title="Login" > </Button></View>
);
}
从“React”导入React;
从“react native”导入{View,Button};
从“../../services/api”导入api
导出默认函数登录(){
异步函数handleSubmit()
{
const response=wait api.post(“/login”{
电邮:'nowahmst@gmail.com',
密码:'123456',
});
}
返回(
);
}
后端正在运行,我可以通过邮递员或失眠症获得响应,并且我的路由设置为post以及api请求 似乎存在未处理的错误,如果将
handleSubmit
函数包装在try-catch
语句中,则应该能够记录错误。此外,还有一个键问题
表明无法访问服务器,可能您的地址或端口错误。地址和端口正确。console.error(e.message)返回未定义的错误