Cordova 中的错误 "The connection to the server was unsuccessful." 和 jQuery
Error "The connection to the server was unsuccessful." in Cordova and jQuery
我使用 Cordova 和 JQuery 移动设备为 android 创建了一个应用程序。当我使用 google chrome 测试 运行 代码时效果很好,但是当我尝试使用 android studio 在 android 模拟器上 运行 它时在 cmd
(locate>cordova emulate android
) 中它不起作用。
当我尝试在模拟器上 运行 它时,我得到:
"The connection to the server was unsuccessful. (file:///android_asset/www/index.html)"
但如果我不使用 JQuery,它工作正常。我没有修改 JQuery 或 JQuery 移动版中的任何代码。
<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
(在阅读了一些旧文件后修改了文件 post 但仍然不起作用)
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
当网页未加载/无法访问时会出现此错误。
要解决此问题,请创建一个新的 html 文件
main.html
<!doctype html>
<html>
<head>
<title>tittle</title>
<script>
window.location='./index.html';
</script>
<body>
</body>
</html>
并在 config.xml
中将发布 URL 保持为 main.html
// Set by <content src="main.html" /> in config.xml
loadUrl(launchUrl);
这将修复
我首先建议,检查您的 config.xml 有一个部分,您可以在其中指定应用程序的起始页。它一定是跳到 "index.html",我认为你的页面叫做 "Login.html"(只是猜测)
该部分看起来像这样
<content src="your_page.html" />
只需更改该部分并尝试。
如果此部分设置正确,则 Mohhamed Imran N 的方法有效,我已经对其进行了测试。
希望这有帮助。
我用过以下:
<content src="./index.html" />
只需在页面地址前添加./
即可!
我使用 Cordova 和 JQuery 移动设备为 android 创建了一个应用程序。当我使用 google chrome 测试 运行 代码时效果很好,但是当我尝试使用 android studio 在 android 模拟器上 运行 它时在 cmd
(locate>cordova emulate android
) 中它不起作用。
当我尝试在模拟器上 运行 它时,我得到:
"The connection to the server was unsuccessful. (file:///android_asset/www/index.html)"
但如果我不使用 JQuery,它工作正常。我没有修改 JQuery 或 JQuery 移动版中的任何代码。
<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
(在阅读了一些旧文件后修改了文件 post 但仍然不起作用)
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
当网页未加载/无法访问时会出现此错误。
要解决此问题,请创建一个新的 html 文件
main.html
<!doctype html>
<html>
<head>
<title>tittle</title>
<script>
window.location='./index.html';
</script>
<body>
</body>
</html>
并在 config.xml
中将发布 URL 保持为 main.html// Set by <content src="main.html" /> in config.xml
loadUrl(launchUrl);
这将修复
我首先建议,检查您的 config.xml 有一个部分,您可以在其中指定应用程序的起始页。它一定是跳到 "index.html",我认为你的页面叫做 "Login.html"(只是猜测) 该部分看起来像这样
<content src="your_page.html" />
只需更改该部分并尝试。 如果此部分设置正确,则 Mohhamed Imran N 的方法有效,我已经对其进行了测试。 希望这有帮助。
我用过以下:
<content src="./index.html" />
只需在页面地址前添加./
即可!