weixin_33720956 2015-07-17 13:19 采纳率: 0%
浏览 125

AJAX在手机上失败

I am having an issue with AJAX requests on mobile browsers (and iPads, etc) but it works fine on desktop computers. I am at a complete loss to work out what's wrong here.

var xmlhttp;
if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
}else{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == XMLHttpRequest.DONE ){
       if(xmlhttp.status == 200){
            console.log(xmlhttp.responseText);
       }else{
           alert("STATUS "+xmlhttp.status);
       }
    }
}
xmlhttp.open("GET","http://www.mywebsite.co.uk/assets/php/upvote.php?id="+id,true);
xmlhttp.send();

I've also tried: xmlhttp.open("GET","/assets/php/upvote.php?id="+id,true); and xmlhttp.open("GET","../php/upvote.php?id="+id,true); All three of which work on a desktop computer (console logs a success message) but on mobile devices it alerts "STATUS 0".

I can't work out why this works fine on desktops but xmlhttp.status is 0 on mobiles.

  • 写回答

1条回答 默认 最新

  • weixin_33671935 2015-07-17 14:03
    关注

    OK, thanks for all your help!

    I loaded up iOS Safari DevTools and watched the console. I was getting a cross site scripting issue even though I'd changed my path to a '/assets/php/...'.

    This is due to a .htaccess file forcing all traffic through 'www.' but it wasn't acting on both directories that my initial webpage and my AJAX files were in (so some path did and some paths didn't have 'www.').

    I've updated my .htaccess (which for some reason wasn't acting on iOS Safari requests only desktop Chrome ones) and now it works fine!

    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上