【问题标题】:CORS on phonegap not workingphonegap 上的 CORS 不起作用
【发布时间】:2014-06-06 06:30:54
【问题描述】:

我终于让代码通过浏览器从我的 PC 跨域工作到我的服务器。但是将代码移动到phonegap它不再起作用了。我已授予互联网和访问来源*的许可,因此它应该能够访问所有网页。我还需要启用什么?

<html>
<head>
<script src="js/jquery-1.11.0.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<script>
    function dologin() {
        var senddata = $('#form').serialize();
        alert(senddata);

        $.ajax({
            type: 'post',
            url: 'XXXXXXXX',
            data: senddata,
            crossDomain: true,
            dataType: 'json',
            success: function(response) {
                if(response.success == true)
                    alert("YAY");
                else
                    alert("NAY");
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {      
                console.log("Status: " + textStatus); alert("Error: " + errorThrown);
            } 
        });
        return false;
    };
    </script>
</head>
<body>
    <h1>Logg inn</h1>
    <form id="form" onsubmit="return dologin();">
        <div class="form-group">
            <label for="email">Epost</label> <input type="email"
                class="form-control" name="email" value="test@test.no"
                placeholder="Epost">
        </div>
        <div class="form-group">
            <label for="password">Passord</label> <input type="password"
                class="form-control" name="password" value="test"
                placeholder="Passord">
        </div>
        <div class="checkbox">
            <label> <input type="checkbox" name="remember_me">
                Husk meg
            </label>
        </div>
        <button type="submit" class="btn btn-primary">Logg inn</button>
    </form>

    <div class="login-help">
        <p>
            Glemt passordet? <a href="index.html">Trykk her for å endre det</a>.
        </p>
    </div>
</body>
</html>

【问题讨论】:

  • 您在哪个平台上尝试?安卓还是IOS?
  • @ConnectingCode: 这是安卓
  • 你到底得到了什么错误部分?
  • @ConnectingCode:我在警报中收到错误“未找到”。我在 PC 上遇到了同样的错误,直到我在服务器上添加了一些启用 CORS 的行。
  • 在这里查看我的答案:stackoverflow.com/questions/22970648/…

标签: javascript ajax cordova cors


【解决方案1】:

尝试在 ajax 调用中包含这一行

headers: { "cache-control": "no-cache" }

【讨论】:

  • 你点击的网址是http还是https?
  • 只是普通的http,后面有一个php处理东西(Laravel框架)
  • 在浏览器中它工作正常吗?因此,请检查该手机的互联网连接是否存在任何问题。
  • 跨域策略不适用于 PhoneGap(出于各种原因,主要是因为您的应用程序本质上是在设备上运行 file:// URI)。所以你尝试删除那些。
  • 它在我电脑上的浏览器中运行良好。但我不明白你上一篇文章的意思,删除了什么?
【解决方案2】:

跨域策略不适用于 PhoneGap(出于多种原因,主要是因为您的应用程序本质上是在设备上的 file:// URI 上运行的)。因此,您尝试删除 ajax 调用中的跨域参数。

【讨论】:

    猜你喜欢
    • 2015-08-26
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多