在cordova应用程序中的Ajax请求

时间:2018-06-01 21:37:47

标签: cordova

我正在使用cordova。我想在我的应用中使用$ .ajax jquery请求。这是我的“Content-Security-Policy”元标记:

<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 'self' http://localhost">

我的应用中有一个链接:

<div data-role="page" id="login_page" data-theme="a" >
            <div data-role="main" class="ui-content">
                        <button data-role="button" id="get_web_data" data->icon="lock" data-transition="fade" >Get Web data</button>
            </div>
        </div> <script type="text/javascript" src="js/jquery_2.1.1.js"></script>
        <script type="text/javascript" src="rtl-jquery-mobile-master/js/rtl.jquery.mobile-1.4.0.js"></script>

这是我的jquery代码:

 $('#get_web_data').on('tap',function(){
        $.ajax({
            url:'http://localhost/test.php',
            type:'GET',
            data:{'user_name':'amir','password':'123'},
            dataType:'json',
            success:function(data){
                alert(data);
            },
            error:function(error){alert('Error');}
        }) ;
     });

点击链接后,我看到了这个错误:

  

index.html:1无法加载   http://localhost/test.php?user_name=amir&password=123:不   请求中存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://localhost:8000”   访问。

我的问题在哪里?

2 个答案:

答案 0 :(得分:0)

将其添加到PHP文件的第一行。 header('Access-control-allow-origin: *')

查看CORS了解更多详情。基本上该请求不受服务器信任,因此它否认。通过设置CORS,您可以允许所有请求

答案 1 :(得分:0)

我更改了script.js文件,现在每件事都有效。

{content-start}
abc1
labtest
def1
ghi1
{content-end}
{content-start}
abc2
def2
labtest
ghi2
{content-end}
{content-start}
abc3
def3
ghi3
{content-end}