当我使用 Postman 时,我可以成功地向这个地址发送请求:https://localhost:9001/api/Account/authenticate
。
但是,我的 react-native 应用程序针对相同的请求抛出错误:
<块引用>类型错误:网络请求失败
这是我的请求代码:
_login() {
var api = API.concat("api/Account/authenticate");
api = 'https://localhost:9001/api/Account/authenticate';
fetch(api, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
body: JSON.stringify({
'email': 'superadmin@gmail.com',
'password': '123Pa$$word!',
}),
})
// .then((response) => response.json())
.then((res) => {
console.log("res.status", res.status);
console.log('data: ', res.data);
// console.log('jwToken: ', res.data.jwToken);
console.log('message: ', res.message);
console.log('api:', api);
});
}
答案 0 :(得分:0)
尝试添加
<uses-permission android:name="android.permission.INTERNET"/>
在您的 adroid/app/src/main 目录中的 AndroidManifest.xml 中
Android 模拟器或设备都有自己的操作系统,因此您无法通过 localhost
访问它。
尝试改变
https://localhost:9001/api/Account/authenticate
到
https://10.0.2.2:9001/api/Account/authenticate
答案 1 :(得分:0)
试试下面的修改:
login = () => {
var api = API.concat("api/Account/authenticate");
api='https://localhost:9001/api/Account/authenticate';
fetch({
url: api,
method: 'POST',
headers: {
Accept: 'application/json',
},
body: JSON.stringify({
email: 'superadmin@gmail.com',
password: '123Pa$$word!',
}),
})
.then((res) => {
console.log("res.status", res.status);
console.log('data: ', res.data);
// console.log('jwToken: ', res.data.jwToken);
console.log('message: ', res.message);
console.log('api:', api);
})
.catch(err => {
console.log('Error : ', err.message)
})
}
答案 2 :(得分:0)
有效
_login() {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"email": "superadmin@gmail.com",
"password": "123Pa$$word!"
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
var api = API.concat("api/Account/authenticate");
fetch(api, requestOptions)
.then(response => response.json())
.then(result => {
console.log('succeeded: ', result.succeeded);
console.log('message: ', result.message);
console.log('errors : ', result.errors);
console.log("res", result);
console.log('data: ', result.data);
console.log("res.data.jwtoken: ", result.data.jwToken);
console.log('api:', api);
})
}