PhoneGap AJAX request not working

时间:2015-10-30 21:55:57

标签: android jquery ajax cordova phonegap-build

I am using latest PhoneGap version, and doing ajax calls.

$.ajax({
            type      : 'POST',
            url       : 'http://example.com/path to my API', 
            data      : {d1: v1, d2: v2},
            dataType  : 'json',
            success   : function(data) {
                           console.log(data);
                        },
            error     : function(){
                            alert('Error');
                        }
}); 

When I test my app on desktop browser or mobile PhoneGap Developer App, it works fine, but after building application (.apk), ajax requests do not work and fall on failure rather than success. I have done all the configurations mentioned which include:

<access origin="*" />
 <plugin name="cordova-plugin-whitelist" version="1" />
    <allow-navigation href="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />

Also added Content-Security-Policy meta tags in my HTML and pointed to domain where my API is located:

<meta http-equiv="Content-Security-Policy" content="connect-src 'self' http://example.com/">

I am using PhoneGap online build service to build app. I really need help as I have done all the fixes which I could find but no success yet.

1 个答案:

答案 0 :(得分:1)

@Nabeel, 你很幸运我目前正在完成blog post on whitelist

按如下方式更改您的CSP。它应该做的伎俩

<meta http-equiv="Content-Security-Policy" 
          content="default-src *; 
                   style-src 'self' 'unsafe-inline' 'unsafe-eval'; 
                   script-src 'self' 'unsafe-inline' 'unsafe-eval';">

如果您使用的是iOS9,则需要为Apple的ATS添加内容。请进一步阅读该文档以获取详细信息。

最好的运气