api - 将本机获取请求反应到.net core 3 api网络请求失败
问题描述
我们遇到了一个问题,我们使用 3 种不同的 req 方法(fetch、axios、frisbee)来自运行在 android 模拟器上的 react 本机应用程序。
服务器日志建议服务器接收请求,对其进行验证并响应 200ok
但是本机反应显示类型错误网络错误有时会产生错误
有什么建议么?
代码:
handleLoginUser() {
axios({
method: 'post',
url: 'https://example.com/users/login',
data: {
email: 'xyz@gmail.com',
password: '1234567890'
},
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
.then(function(response) {
console.log(response);
response.json();
})
.catch(function(response) {
console.error(response);
});
fetch('https://example.com/users/login', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
email: 'ec223e@gmail.com',
password: '1234567890'
})
})
.then(response => {
console.log(response);
response.json();
})
.then(result => {
if (result.success) {
alert("Login successful");
} else
alert("Unable to Login");
}).catch(function(response) {
console.error(response);
});
const Frisbee = require('frisbee');
const api = new Frisbee({
baseURI: 'https://example.com', // optional
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: {
email: 'aaasdkgur@gmail.com',
password: '1234567890'
}
});
api.post('/users/login')
.then(response => {
console.log(response);
response.json();
})
.then(result => {
if (result.success) {
alert("Login successful");
} else
alert("Unable to Login");
}).catch(function(response) {
console.error(response);
});
}
调试堆栈
axios
"TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27200:18)
at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:32754:27)
at XMLHttpRequest.setReadyState (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31838:20)
at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31665:16)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31775:47
at RCTDeviceEventEmitter.emit (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:6075:37)
at MessageQueue.__callFunction (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5503:44)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5216:17
at MessageQueue.__guard (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5457:13)
at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5215:14)"
拿来
"TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:27200:18)
at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:32754:27)
at XMLHttpRequest.setReadyState (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31838:20)
at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31665:16)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31775:47
at RCTDeviceEventEmitter.emit (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:6075:37)
at MessageQueue.__callFunction (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5503:44)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5216:17
at MessageQueue.__guard (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5457:13)
at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5215:14)"
飞盘
"Error: Network Error
at createError (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:122025:17)
at XMLHttpRequest.handleError (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:121933:16)
at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:32754:27)
at XMLHttpRequest.setReadyState (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31838:20)
at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31665:16)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:31775:47
at RCTDeviceEventEmitter.emit (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:6075:37)
at MessageQueue.__callFunction (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5503:44)
at http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5216:17
at MessageQueue.__guard (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:5457:13)"
服务器日志
2019-12-12 16:44:32.457 +03:00 [INF] Request starting HTTP/2.0 POST https://example.com/users/login application/json 55
2019-12-12 16:44:32.457 +03:00 [INF] Request starting HTTP/2.0 POST https://example.com/users/login application/json; charset=utf-8 52
2019-12-12 16:44:32.460 +03:00 [INF] Executing endpoint 'c.Api.Controllers.UsersController.LoginUser (c.Api)'
2019-12-12 16:44:32.460 +03:00 [INF] Executing endpoint 'c.Api.Controllers.UsersController.LoginUser (c.Api)'
2019-12-12 16:44:32.461 +03:00 [INF] Route matched with {action = "LoginUser", controller = "Users"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.ActionResult`1[c.Api.Models.Users.LoginUserResultModel] LoginUser(c.Api.Models.Users.LoginUserModel) on controller c.Api.Controllers.UsersController (c.Api).
2019-12-12 16:44:32.461 +03:00 [INF] Route matched with {action = "LoginUser", controller = "Users"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.ActionResult`1[c.Api.Models.Users.LoginUserResultModel] LoginUser(c.Api.Models.Users.LoginUserModel) on controller c.Api.Controllers.UsersController (c.Api).
2019-12-12 16:44:32.462 +03:00 [INF] LoginUser() started for - aaasdkgur@gmail.com
2019-12-12 16:44:32.462 +03:00 [INF] LoginUser() started for - ec223e@gmail.com
2019-12-12 16:44:32.473 +03:00 [INF] Request starting HTTP/2.0 POST https://example.com/users/login application/json; charset=utf-8 52
2019-12-12 16:44:32.474 +03:00 [INF] Executing endpoint 'c.Api.Controllers.UsersController.LoginUser (c.Api)'
2019-12-12 16:44:32.475 +03:00 [INF] Route matched with {action = "LoginUser", controller = "Users"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.ActionResult`1[c.Api.Models.Users.LoginUserResultModel] LoginUser(c.Api.Models.Users.LoginUserModel) on controller c.Api.Controllers.UsersController (c.Api).
2019-12-12 16:44:32.475 +03:00 [INF] LoginUser() started for - xyz@gmail.com
2019-12-12 16:44:32.758 +03:00 [INF] Executing ObjectResult, writing value of type 'c.Api.Models.Users.LoginUserResultModel'.
2019-12-12 16:44:32.758 +03:00 [INF] Executed action c.Api.Controllers.UsersController.LoginUser (c.Api) in 297.38800000000003ms
2019-12-12 16:44:32.759 +03:00 [INF] Executed endpoint 'c.Api.Controllers.UsersController.LoginUser (c.Api)'
2019-12-12 16:44:32.759 +03:00 [INF] Request finished in 302.1514ms 200 application/json; charset=utf-8
2019-12-12 16:44:32.812 +03:00 [INF] Executing ObjectResult, writing value of type 'c.Api.Models.Users.LoginUserResultModel'.
2019-12-12 16:44:32.812 +03:00 [INF] Executed action c.Api.Controllers.UsersController.LoginUser (c.Api) in 337.31350000000003ms
2019-12-12 16:44:32.812 +03:00 [INF] Executed endpoint 'c.Api.Controllers.UsersController.LoginUser (c.Api)'
2019-12-12 16:44:32.812 +03:00 [INF] Request finished in 339.45410000000004ms 200 application/json; charset=utf-8
2019-12-12 16:44:32.966 +03:00 [INF] Executing ObjectResult, writing value of type 'c.Api.Models.Users.LoginUserResultModel'.
2019-12-12 16:44:32.966 +03:00 [INF] Executed action c.Api.Controllers.UsersController.LoginUser (c.Api) in 504.8709ms
2019-12-12 16:44:32.966 +03:00 [INF] Executed endpoint 'c.Api.Controllers.UsersController.LoginUser (c.Api)'
2019-12-12 16:44:32.966 +03:00 [INF] Request finished in 508.91540000000003ms 200 application/json; charset=utf-8
解决方案
推荐阅读
- php - 在 Woocommerce 中禁用特定优惠券代码的“购物车需要付款”
- node.js - Supertest/Jest 检查标头是否存在
- mysql - 在 Maria DB 中,我可以将 CASE 与表一起使用吗?
- html - 使用 flexbox 以相同的高度均匀分布导航项目
- javascript - 如何在 React 中导入图像文件夹,其中文件中的每个单独图像都可以以状态存储?
- python - 如何从网站上刮取结果的名称?
- jsonschema - 带有 AJV 的扩展模式
- angular - 无法跨 CORS 策略 - 具有弹簧启动安全性的 Angular
- regex - 除非存在某个子字符串,否则如何将所有行与某个模式匹配?
- svm - 使用 spacy 的三个实体之间的最短路径依赖