【问题标题】:Ajax json cross domain request not working with cordova appAjax json跨域请求不适用于cordova应用程序
【发布时间】:2016-01-29 01:03:16
【问题描述】:

我正在做科尔多瓦安卓应用。在这个我想要由 REST api 验证的用户登录功能。但在移动设备中,它给出了 ajax 错误代码 '0' 和 throwedError 空白。我已将我的设备与桌面系统连接以测试应用程序。

在桌面浏览器中运行正常。 我使用了下面的代码,

apiurl = "http://xxx.xxx.xxx.xxx/restapi/"; 
$.ajax({
                url: apiurl+'user-signin/login',
                type: 'POST',
                dataType :'json'
                crossDomain: true,
                beforeSend:function(){ $.mobile.loading( 'show' ); },
                complete: function() { $.mobile.loading( 'hide' )  }, 
                success: function (res) {
                    alert(res.status);
                    if(res.status == "success") {
                        goInsert(res.result.user_id, res.result.user_name, res.result.user_password);
                        window.location="dashboard.html";
                    } else if(res.status == "failed") {
                        alert(res.msg);
                        //alert(message[res.msg]);
                    } 
                },
                error: function(xhr, textStatus, thrownError) {
                    alert(xhr.status+":"+textStatus+":"+thrownError);
                    navigator.app.exitApp();
                },
                data: new FormData($('#loginfrm')[0]),
                cache: false,
                contentType: false,
                processData: false

            });

我已经在我的 rest api 中添加了“header("Access-Control-Allow-Origin: *");”。

我也使用了数据类型“jsonp”,但它也不适合我。

有人可以帮我吗?

【问题讨论】:

    标签: android jquery json ajax cordova


    【解决方案1】:

    如果您使用的是 Cordova 5,那么您可能需要查看您的内容安全策略,它是 index.html 中的一个元标记 - 默认情况下,除非您修改它以包含您的 API 的 URL,否则它不会允许这样做。

    尝试修改元标记(如果不存在,则添加一个),如下所示:

    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://xxx.xxx.xxx.xxx/">
    

    要更深入地了解这个,请查看http://moduscreate.com/cordova-5-ios-9-security-policy-changes/

    【讨论】:

    • 嗨,提交表单时,应用程序中出现“加载页面错误”。
    猜你喜欢
    • 2016-10-19
    • 2018-08-10
    • 2014-12-30
    • 2016-07-16
    • 2014-04-18
    • 2013-03-02
    • 2013-12-01
    • 1970-01-01
    • 2013-10-19
    相关资源
    最近更新 更多