Phonegap应用程序不执行ajax调用

时间:2016-12-20 08:39:28

标签: android jquery ajax cordova

我有使用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调用外部后端)文件加载到桌面浏览器中时,一切正常。 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  1. 使用deviceready listener再次测试。
  2. 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. 检查您的内容元数据。

      &lt; meta http-equiv =&#34; Content-Security-Policy&#34; content =&#34; default-src *; style-src&#39; self&#39; &#39;不安全直插&#39 ;; script-src&#39; self&#39; &#39;不安全直插&#39; &#39;不安全-EVAL&#39;&#34;&GT;