net :: ERR_CONNECTION_TIMED_OUT错误来自真实设备上的cordova应用程序的请求 [英] net::ERR_CONNECTION_TIMED_OUT error in request from cordova app on real device

查看:1050
本文介绍了net :: ERR_CONNECTION_TIMED_OUT错误来自真实设备上的cordova应用程序的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在chrome浏览器和(genymotion)模拟器上正常运行的应用程序.

但是,如果我在智能手机上运行它,则对服务器的每个请求(发布,获取,放置)都会给出结果:Failed to load resource: net::ERR_CONNECTION_TIMED_OUT

我的设置:

科尔多瓦:5.2.0

config.xml:

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

jquery:

$.support.cors=true;

我的要求:(我对主干网也做了同样的事情,结果也一样)

$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);
                });

我已经通过导出和运行来运行我的应用程序(cordova运行android),并且我的手机具有无线连接.

解决方案

@Asqan,
您犯了一个常见错误,列在顶部Cordova/Phonegap新手开发人员的错误.在这种情况下,您需要将white-list插件和CSP添加到您的网页.

替代方法是此快速修复程序–但是知道此修补程序消除了white-list的所有需求.这会创建一个安全性问题,您可能不想通过.

快速修复,将此添加到您的config.xml
<preference name="phonegap-version" value="3.7.0" />

长答案是这样的:

来自 Cordova新手开发人员的主要错误/Phonegap 您已点击:

  • #6 未为编译器设置电话间隙版本"
  • #7 未为您的插件设置版本"
  • #10 未在config.xml中添加新的白名单"和白名单插件"参数.

对于#6& #7

使用CLI版本时,如果未为平台分配版本,或者未在config.xml中设置phonegap-version,则在"Phonegap Build"中分配了最新版本.如果幸运的话,您的程序将按预期运行.如果您不走运,则会出现一系列级联错误.

幸运的是,我们所有人Holly Schinsky都写了一篇不错的博客文章来解释这一切:

Cordova/PhoneGap版本混乱
http://devgirl.org/2014/11/07/cordovaphonegap-version-混乱/

对于#10

此相对* NEW *要求表示–要访问任何网站或网络上的资源,您必须使用白名单和白名单插件.如果您使用的是cordova-android@4.0.0或更高版本,则此要求生效.包括cli-5.1.1.但是,如果您的版本低于4.0.0,例如3.5.0或3.7.0,那么您将不必添加白名单要求.

需要明确的是,白名单"已经存在了一段时间,但是插件和要求是非常新的.如您所料,添加白名单"后,事实上的开放式访问功能已被弃用.或以另一种方式说,事实上的开放式访问功能已被计划并计划取消.此更改标志着取消了开放式访问功能的一步.

此外,内容安全政策(CSP)吸引了众多开发人员-因为它的宣传不力. 根据您的使用和所使用的Phonegap的版本,CSP需要进入您使用的每个HTML页面,就像您必须等待"deviceready"一样.但是,在某些情况下根本不需要它.该文档有些混乱,请仔细阅读.该文档埋在许多最新文档页面的底部.

相关链接

Phonegap构建论坛:科多瓦白名单指南

  • Phonegap白名单指南
  • 《 Phonegap构建白名单指南》
  • 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.

    解决方案

    @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

    这篇关于net :: ERR_CONNECTION_TIMED_OUT错误来自真实设备上的cordova应用程序的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆