【问题标题】:Cordova JQuery AJAX not workingCordova JQuery AJAX 不工作
【发布时间】:2015-10-08 19:14:48
【问题描述】:

在谷歌搜索几个小时后,我必须写下,cordova(CLI 5.3.3) 应用程序在通过 jquery AJAX 调用时返回页面未找到。

我已经按照白名单插件(https://github.com/apache/cordova-plugin-whitelist) 中的所有步骤进行操作,但仍然没有运气。

我已经在 config.xml 中包含了这些行

<access origin="*" />
<allow-navigation href="*" />

还包括 CSP 之类的

<meta http-equiv="Content-Security-Policy" content="default-src 'self' https:">

AJAX 请求之类的

$.ajax({
                    beforeSend: function() { $.mobile.loading("show"); }, //Show spinner
                    complete: function() { $.mobile.loading("hide"); }, //Hide spinner
                    url: weburl+"lgoin.php",                        
                    data: { email: $("#txtemail").val(), password: $("#txtpassword").val()},
                    type: "POST",                       
                    success: function(data) {                       
                        var response=$.parseJSON(data);                         
                        }                           
                    },                      
                    error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        alert(msg);
                    },
                });

AJAX 请求总是以错误回调结束,例如 “未找到请求的页面。[404]”

注意:-- 我已经使用 Avance REST API 扩展测试了 web 服务并且运行良好

谁能帮我解决这个问题。

提前感谢您的时间和考虑。 -奈蒂克

【问题讨论】:

  • 总是 404,还是其他错误?
  • 也许您的服务器对设备不公开可以连接到服务。您可以通过在设备中打开浏览器并输入 url + 参数来测试并查看结果。如果在浏览器中可以正常工作,请检查 AndroidManifest.xml 文件(在 Android 中)并确保 应用程序可以通过互联网访问。
  • @usherwood 总是 404。在 cordova cli 3.8 上运行的其他 phonegap 应用程序(具有相同框架的旧应用程序)在同一台服务器上运行良好。请求需要时间,所以它调用服务器但总是返回 404,尽管服务是存在(服务器在 HTTPS 上运行)。
  • 适用于 http 服务但不适用于 https 服务
  • 也许你的链接不正确,因为你拼写 login.php 错误

标签: jquery ajax cordova jquery-mobile


【解决方案1】:

我刚刚在 AndroidManifest.xml 的 application 标签中添加了这一行

android:usesCleartextTraffic="true" 

它对我有用

【讨论】:

  • 我希望我能 ☻
【解决方案2】:

基本上,问题是您应该更新cordova-whitelist-plugin。

插件可以安装

cordova plugin add cordova-plugin-whitelist

或添加

&lt;plugin name="cordova-plugin-whitelist" spec="1" /&gt;

到config.xml,然后配置

&lt;allow-navigation href="*" /&gt;

代替旧的&lt;access origin="*" /&gt; 标签。

请看here for details

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2010-12-23
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多