XMLHttpRequest什么都不做......?

时间:2014-02-05 20:37:13

标签: javascript php jquery ajax

我是javascript和AJAX的新手,并且花了最后8个小时处理这个问题,并且它击败了我。我知道它简单,只是找不到我做错了什么。我的网站上有一个带有on-click = SendCommand()的图像。这是我的js代码

function SendCommand(){
        alert("BingoBango!");

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","https://www.justanexample.com/API/MobileAPP/SendCommand.php?apikey=7785adf3a5d3a3adsf555nb5v55bsaer5v&mac=b827eb6ffa19&command=2",true);
        xmlhttp.send();
};

我收到警告消息,并且使用firebug或chrome javascript控制台我没有错误。但是该页面未执行。然而,我可以将确切的URL复制并粘贴到浏览器中,并且它可以成功执行。

任何帮助都会非常感激,它会引起我的注意。

2 个答案:

答案 0 :(得分:1)

  

它正在调用的页面是调用python脚本以及更新mysql记录。在我的请求中我可以做些什么来要求它不使用浏览器缓存并实际点击我的服务器吗?

对于在服务器上执行操作的内容,不应使用GET请求。请改用POSTshould not be cached

如果这没有帮助,请调整您的HTTP cache headers,或作为最后的手段,将随机字符串附加到网址。

答案 1 :(得分:0)

我就是这样做的

JS

function SendCommand()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","https://www.justanexample.com/API/MobileAPP/SendCommand.php?apikey=7785adf3a5d3a3adsf555nb5v55bsaer5v&mac=b827eb6ffa19&command=2",true);
xmlhttp.send();
}

HTML

<button type="button" onclick="SendCommand()">My button</button>
<div id="myDiv"></div>