$ .ajax POST返回"状态":403," statusText":"禁止" cordova android

时间:2015-02-05 07:30:02

标签: android ajax cordova jquery-mobile phonegap-plugins

我正在使用jquery.mobile-1.4.3.js和cordova.3.5.0开发Android phonegap应用程序。

我正在为web服务调用$ ajax。下面是一段代码片段。

 $.ajax({
        type: 'POST',
        data: LoginData,
        crossDomain:true,
        dataType : 'json',
        timeout: 50000,
        url: 'https://dsp-wasatchtechies.cloud.dreamfactory.com/rest/user/session?app_name=XXXXX',
        success: function(data) {


            console.log(' SESSION' + JSON.stringify(data));

        },
        error: function(data) {

            //ActivityIndicator.hide();
            console.log('ERROR : SESSION' + JSON.stringify(data));
            //ShowAlertMessage('There was an error while SESSION');
        }
    });

它完美无缺,但现在它的回应:

  [INFO:CONSOLE(155)] "ERROR SESSION{"readyState":4,"responseText":"","status":403,"statusText":"Forbidden"}", source: file:///android_asset/www/js/index.js (155)

这段代码有什么问题?为什么它不起作用? 任何帮助,建议将不胜感激

提前谢谢。

2 个答案:

答案 0 :(得分:2)

  1. 确保服务器代码返回授予其他站点访问权限的CORS标头:
  2.   

    例如:标题(' Access-Control-Allow-Origin:*');

    1. 确保" ModSecurity"在您的服务器上被禁用(如果您有cPanel访问权限,您应该可以通过cPanel / Security或类似的方式执行此操作)

    2. 确保您安装了cordova白名单插件(使用" legacy"插件用于cordova pre v5.0.0):

      $ cordova插件添加cordova-plugin-legacy-whitelist

    3. 在cordova config.xml中设置一个非常开放的白名单:

      < allow-intent href =" *" />

      < access origin =" *" />

    4. 在index.html中设置内容安全策略:

      < meta http-equiv =" Content-Security-Policy" content =" default-src *; script-src *' unsafe-eval' '不安全直插&#39 ;; connect-src *; img-src *; style-src *' unsafe-inline' ; media-src *;">

    5. 这使得所有内容都可以打开,而跨域域,列入白名单的URL请求和内容安全策略的要点是限制跨域访问。在你让事情发生之后,我会把它留作研究和限制安全的练习。

      我希望这会有所帮助。

      参考文献:

      https://github.com/apache/cordova-plugin-whitelist#content-security-policy https://cordova.apache.org/announcements/2015/04/21/plugins-release-and-move-to-npm.html http://content-security-policy.com/

答案 1 :(得分:0)

您是否拥有在您的APP允许的域上尝试访问的域名?您可以在comfig.xml上设置它。