net::ERR_CONNECTION_TIMED_OUT error in request from cordova app on real device

回眸只為那壹抹淺笑 提交于 2019-12-24 11:34:56

问题


I have an application which runs well on chrome browser and on (genymotion) emulator.

However, if i run it on my smartphone, each request to server (post, get, put) gives the result : Failed to load resource: net::ERR_CONNECTION_TIMED_OUT

My settings:

Cordova: 5.2.0

config.xml:

    <content src="index.html" />
    <access origin="*" />
    <allow-navigation href="*" />
    <allow-intent href="*" />

jquery:

$.support.cors=true;

my request: (i've done the same also with backbone, the same result)

$scope.loginRequestObject = $scope.getLoginObject();
$.ajax({
                url : $scope.baseUrl + "/myTable",
                type : 'post',
                contentType : 'application/json; charset=utf-8',
                scriptCharset : "utf-8",
                data : JSON.stringify($scope.loginRequestObject),
                dataType : 'json',
                fail : function(a,b,c){
                    console.log(a);
                    console.log(b);
                    console.log(c);
                },
                success: function(a,b,c){
                    console.log(a);
                    console.log(b);
                    console.log(c);
                });

I've runned my app by both exporting and running (cordova run android) and my phone has a wireless connection.


回答1:


@Asqan,
you have made a common mistake which is listed in Top Mistakes by Developers new to Cordova/Phonegap. In this instance, you need to add the white-list plugin, and CSPto your webpage.

The alternative is this quick fix – but know that this fix removes all needs for white-list. This creates a security issue which you may not want to by pass.

QUICK FIX Add this to your config.xml
<preference name="phonegap-version" value="3.7.0" />

The long answer is as such:

From Top Mistakes by Developers new to Cordova/Phonegap you have hit:

  • #6 Not setting the "phonegap version" for your compiler
  • #7 Not setting "version" for you plugins
  • #10 Not adding the new "white-list" and "white-list plugin" parameters in config.xml.

For #6 & #7

With the CLI version, if you do not assign a version for your platform OR in ''Phonegap Build'' if you do not set the phonegap-version in config.xml, YOU WILL GET THE LATEST VERSION. If you are lucky, your program just works as expected. If you are not lucky, you'll get a set of cascading errors.

Luckily for all of us, Holly Schinsky has written a nice blog post to explain it all:

Cordova/PhoneGap Version Confusion
http://devgirl.org/2014/11/07/cordovaphonegap-version-confusion/

For #10

This relatively * NEW * requirement means – to access ANY website or resources on the web, you MUST use the whitelist and the whitelist plugin. This requirement goes into affect, if you are using cordova-android@4.0.0 or better; including cli-5.1.1. If however, your version is before 4.0.0, let's say 3.5.0 or 3.7.0, then you will not have to add the white-list requirement.

To be clear, the "whitelist" has been around for a bit, but the plugin and requirement is very new. As you would expect, when the "whitelist" was added, the defacto open-access feature was deprecated. Or said another way, the defacto open-access feature was planned and scheduled to be eliminated. This change marks a step in removal of the open-access feature.

In addition, the Content Security Policy (CSP) has caught numerous developers - because it was soooo poorly publicized. Depending on your use and the version of Phonegap you are using, the CSP needs to go in every single HTML page you used, just like you have to wait for 'deviceready'. However, there are cases where it is not needed at all. The documentation is confusing for some, please read it carefully. The documentation is buried in the bottom of many of the latest documentation pages.

Related Links

Phonegap Build Forum: Notes for upgrading to cli-5.1.1 on PGB and now required Whitelist

  • Cordova Whitelist Guide
  • Phonegap Whitelist Guide
  • Phonegap Build Whitelist Guide


来源:https://stackoverflow.com/questions/33037648/neterr-connection-timed-out-error-in-request-from-cordova-app-on-real-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!