【问题标题】:Phonegap android ajax calls working in browser, not on mobilePhonegap android ajax调用在浏览器中工作,而不是在移动设备上
【发布时间】:2017-02-01 08:51:35
【问题描述】:

我需要一个 phonagap/cordova 应用程序的帮助,它非常基本,但我无法让我的 ajax 调用在我的手机上工作到服务器,在浏览器中一切正常。请注意,我使用 phonegap 的在线构建器来编译我的代码。

我已经尝试了以下所有方法:

  • $.support.cors = 真。
  • 访问来源="*"
  • allow-intent href="*"
  • 允许导航 href="*"

现在我查看了白名单,但由于某种原因,它在添加它时破坏了我的配置文件,然后我尝试了旧的白名单,然后我无法在 phonegap 上在线构建它。

我的 ajax 方法:

$("#btnTest").click(function (evt) {
        var value = "";
        var options = {};
        options.url = "http://*/MobileService/ServiceFile.asmx/test";
        options.type = "POST";
        options.data = JSON.stringify({ "data": value });
        options.dataType = "json";
        options.crossDomain = true;
        options.contentType = "application/json";
        options.success = function (result) { $("#error-msg").text(result.d); };
        options.error = function (err) { $("#error-msg").text("Error saving the data!"); };
        console.log(value);
        $.ajax(options);
        evt.preventDefault();
    });

我的 index.html 头标签:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MobileApp</title>
<meta name="viewport" content="width=device-width, initial-scale=1">   
<link href="css/jquery.mobile-1.4.5.min.css" rel="stylesheet" />
<script src="scripts/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function () {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
});
</script>
<script src="scripts/jquery.mobile-1.4.5.min.js"></script>
<script src="scripts/jSignature/jSignature.min.js"></script>
<link href="css/index.css" rel="stylesheet" />

另请注意,我正在使用 Visual Studio 2013 进行开发。

提前致谢

【问题讨论】:

  • 浏览器控制台中的任何错误?
  • 不,我在浏览器控制台中遇到的唯一错误是当我的手机是链接并且我试图将 json 数据发布到服务器时,它只返回一个 404 错误
  • 这取决于在线构建器对应用程序执行时会发生什么。你试过放一个绝对网址吗?比如localhost:8888等等。

标签: javascript android jquery ajax cordova


【解决方案1】:

所以回答我自己的问题,最后我找到了白名单插件的旧版本。

我将它添加到我的项目后,它就解决了 CORS 错误。

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 2018-05-08
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多