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!

    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败