【问题标题】:Phonegap app does not perform ajax callPhonegap 应用程序不执行 ajax 调用
【发布时间】:2016-12-20 08:39:27
【问题描述】:

我有使用 html + javascript (jquery) 制作的 Android Phonegap 构建应用程序。昨天它工作得很好。今天 ajax 调用不再执行。示例代码:

$(document).ready(function(){
            setTimeout(function(){

                $.ajax({
                    url: urlPrefix + "/xxxxx",
                    dataType: "jsonp",
                    jsonpCallback: "indexCallback"
                });
                alert('Ajax praejo');

            },2000);

        });


        function indexCallback(response) {
            alert('callback prasideda');
        }

显示 ajax 调用后的警报,但未出现 indexCallback 函数中的警报。 Ajax 调用外部后端服务器。我已经在后端服务器中记录了呼叫,但没有出现呼叫。 我试图在ajax调用之前超时,但没有结果。 我在 config.xml 文件中列出了所有可能的域:

<plugin name="cordova-plugin-whitelist" version="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>

<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="data:*" />

当我将 index.html(对外部后端的 ajax 调用)文件加载到桌面浏览器时,一切正常。 有什么想法吗?

【问题讨论】:

  • 嗯,我正在将 html/css/js 文件上传到 phonegap 构建中。在构建应用程序并在安卓手机中运行时。
  • 去掉 jsonpCallback 的引号
  • 它在没有任何变化的情况下开始工作......
  • 似乎是 Wifi 问题。使用移动数据时一切正常,但使用 WIFI 应用程序时无法正常工作。

标签: android jquery ajax cordova


【解决方案1】:

我在 Android 设备上的应用也遇到了类似的问题。经过几天的研究,我发现了blog post。我们的证书颁发机构(前 4 个)的证书是 google webview 中的一个错误的一部分。更新设备上的 webview 和 chrome 后,一切都恢复正常了。

【讨论】:

    【解决方案2】:
    1. 使用 deviceready 侦听器再次测试。

    http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html

    document.addEventListener("deviceready", onDeviceReady, false);
    
    function onDeviceReady() {
            setTimeout(function(){
    
                $.ajax({
                    url: urlPrefix + "/xxxxx",
                    dataType: "jsonp",
                    jsonpCallback: "indexCallback"
                });
                alert('Ajax praejo');
    
            },2000);
    }
    
    function indexCallback(response) {
            alert('callback prasideda');
    }
    
    1. 检查您的内容元数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多