+-
$.ajax POST返回“status”:403,“statusText”:“Forbidden”cordova android
我正在使用 jquery.mobile-1.4.3.js和cordova.3.5.0开发 Android phonegap应用程序.

我打电话给$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)

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

先感谢您.

最佳答案
>确保服务器代码返回授予其他站点访问权限的CORS标头:

eg: header(‘Access-Control-Allow-Origin: *’);

>确保您的服务器上禁用“ModSecurity”(如果您有cPanel访问权限,您应该可以通过cPanel / Security或类似的方式执行此操作)
>确保安装了cordova白名单插件(使用cordova pre v5.0.0的“legacy”插件):

$cordova插件添加cordova-plugin-legacy-whitelist
>在cordova config.xml中设置一个非常开放的白名单:

< allow-intent href =“*”/>

< access origin =“*”/>
>在index.html中设置内容安全策略:

< meta http-equiv =“Content-Security-Policy”content =“default-src *; script-src *'unsafe-eval''unsafe-inline'; connect-src *; img-src *; style-src *'unsafe-inline'; media-src *;“>

这使得WIDE打开所有内容,而跨域域,列入白名单的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/

点击查看更多相关文章

转载注明原文:$.ajax POST返回“status”:403,“statusText”:“Forbidden”cordova android - 乐贴网