首页 > 解决方案 > 将本机获取请求反应到.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

邮递员请求

标签: apireact-nativeasp.net-core.net-coreasp.net-core-webapi

解决方案


推荐阅读