首页 > 解决方案 > got ERR::CONNECTION_TIMED_OUT from cordova to my mvc 5 web api while call from jsfiddle is no issue

问题描述

I got stucked for 2 days regarding this issue. I've deployed mvc 5 web api to my local iis (in my local pc). Then I'd like to call the /token function from my cordova, I always get error timeout.

I've tested it using jsfidle in here and in postman, both return the correct value.

I've installed cordova plugin whitelist, and allow CORS on my api code.

Here's my code to get the access token to my api.

var loginData = {
            grant_type: 'password',
            username: parmUsername,
            password: parmPassword
        };

        $.ajax({
            beforeSend: function (xhrObj) {
                xhrObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            },
            type: 'POST',
            url: 'http://202.189.20.8:8081/api/v1/Token',                
            dataType:'json',
            data: loginData
        })
        .done(function (response, textStatus, jqXHR) {
            window.location.replace('views/mainpage.html');
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        })

I've changed also the data format using below code

var formBody = [];
        for (var property in loginData) {
            var encodedKey = encodeURIComponent(property);
            var encodedValue = encodeURIComponent(loginData[property]);
            formBody.push(encodedKey + "=" + encodedValue);
        }
        formBody = formBody.join("&");

But I still got the same error. I've tried also use cordova plugin advanced http to call the api, but it didn't work.

Please advise, I really confused right now.

标签: angularapicordovamodel-view-controller

解决方案


对不起,请忽略。自己解决了。老实说,这是防火墙问题。2天后,我忘记检查防火墙。可怜的我。:(

阿芬


推荐阅读