donglian1953 2017-03-30 09:17
浏览 53

cordova app中的互联网不一致

I am developing a mobile app using cordova, and I am facing a problem for now 2 days I can't resolve, I am trying to load a simple php file hosted on a server :

<?php 
    echo 'Salut'
?>

And I am trying to read the content while using an ajax call :

$.ajax({
    type: 'get',
    url: 'https://mydomain.so/index.php',
    dataType: 'text',
    success: function (data) { alert(data); },
    error: function (x, e) {
        if (x.status == 0) {
            alert('You are offline!!
 Please Check Your Network.');
        } else if (x.status == 404) {
            alert('Requested URL not found.');
        } else if (x.status == 500) {
            alert('Internel Server Error.');
        } else if (e == 'parsererror') {
            alert('Error.
Parsing JSON Request failed.');
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.
' + x.responseText);
        }
    }
});

But the text show with alert is "You are offline", I do not understand because on the same html page there is a video youtube that I can watch. And I do not know why it's telling me I am offline while I can watch the video... Someone know why I can't read php file but I can watch youtube video ?

Edit: Now I can read my php file inside of my onDeviceReady() function, but if I try to to do while clicking a button, it shows x.status==0, I tried to add :

header('Access-Control-Allow-Origin: *');

in my php file but it's not working :s

Edit2: It's working fine, I was changing the location outside of the success function, I put it inside and it's working perfectly. So I had to add https in my meta tag(before data:) and set new location inside of the ajax request.

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

1条回答 默认 最新

  • dongzuo9096 2017-03-30 09:23
    关注

    It could be possible to get a status code of 0 if you have sent an AJAX call and a refresh of the page was triggered before getting the AJAX response. The AJAX call will be cancelled and you will get this status.

    or

    If you are calling ajax using submit button or on anchor tag, you probably prevent the default behaviour of submit or anchor tag.

    If it is related CORS add following line in your php file.

    header('Access-Control-Allow-Origin: *');
    

    Hope it helps you

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?