【问题标题】:Error "The connection to the server was unsuccessful." in Cordova and jQuery错误“与服务器的连接不成功。”在科尔多瓦和 jQuery 中
【发布时间】:2015-06-15 09:34:14
【问题描述】:

我使用 Cordova 和 JQuery mobile 为 android 创建了一个应用程序。当我使用 google chrome 测试运行代码时它运行良好,但是当我尝试在cmd (locate>cordova emulate android) 中使用 android studio 在 android 模拟器上运行它时它不起作用。

当我尝试在模拟器上运行它时,我得到:

“与服务器的连接不成功。(file:///android_asset/www/index.html)”

但如果我不使用 JQuery,它可以正常工作。我没有修改 JQuery 或 JQuery mobile 中的任何代码。

    <head>
        <-- import jquery and codovar -->
        <link rel="stylesheet" href="./css/jquery.mobile-1.4.5.min.css" />

        <-- if i remove this 3 line of code below program is working as same as open with google chrome -->
        <link rel="stylesheet" href="./css/jquery.mobile-1.4.5.min.css" />
        <script src="./js/jquery-2.1.4.min.js"></script>
        <script src="./js/jquery.mobile-1.4.5.min.js"></script>
        <--end remove here -->

        <script type="text/javascript" src="./js/Login.js"></script>

    </head> 
    <body>
        <div data-role="page" id="page1">
            <div data-role ="header" style="background-color: #00CCA5;">
                <h1 style="color: #FAF0BA; text-shadow: None; text-align: center;">LogIn</h1>
            </div>
            <div data-role ="main" class="ui-content"  >
                <div align="center">
                    <label>id:</label>
                    <input type="text" id="login_id">
                    <br>
                    <label>password:</label>
                    <input type="password" id="login_password">
                    <div id="wrong" style="color: red;"><br></div>
                </div>
                <button style="background-color: #FAF0BA; color:#00CCA5;" data-role ="button" data-icon ="forward" id="let_login">Log in</button>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
    </body>
</html>

这里是login.js

$(document).ready(function () {
    $("#let_login").on("tap",
            function () {
                var id = $("#login_id").val();
                var password = $("#login_password").val();
                if (id == "test" && password == "password") {
                    document.location.href = "./customer.html";
                    //$.mobile.changePage("./customer.html");
                }
                else {
                    $("#wrong").html("Wrong id and/or password");

                }
            });
});

这里是MainActivity.java(在阅读了一些旧帖子后修改了文件,但仍然不起作用)

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //modify line below
        super.setIntegerProperty("loadUrlTimeoutValue", 70000);
        //end modify line 

        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }
}

ps。这是我第一次使用 Cordova 和 JQuery

【问题讨论】:

    标签: android jquery cordova jquery-mobile


    【解决方案1】:

    当网页无法加载/无法访问时会发生此错误。

    要解决这个问题,请创建一个新的 html 文件

    ma​​in.html

    <!doctype html>
    <html>
      <head>
       <title>tittle</title>
       <script>
         window.location='./index.html';
       </script>
      <body>
      </body>
    </html>
    

    并将启动 URL 保留为 config.xml 中的 ma​​in.html

    // Set by <content src="main.html" /> in config.xml
    
    
        loadUrl(launchUrl);
    

    这会解决

    【讨论】:

    • 我只有cordova和插件注入main.html而不是index.html
    【解决方案2】:

    我建议首先检查您的 config.xml 有一个部分,您可以在其中指定应用程序的起始页。它一定是“index.html”,我认为你的页面被称为“Login.html”(只是猜测) 该部分如下所示

    &lt;content src="your_page.html" /&gt;

    只需更改该部分并尝试。 如果此部分设置正确,那么 Mohhamed Imran N 的方法有效,我已经对其进行了测试。 希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      我用过以下:

      <content src="./index.html" />
      

      只需在页面地址前添加./即可!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 1970-01-01
        相关资源
        最近更新 更多