我如何在javascript中发出像www.example.com/example.php?d=a这样的ajax get请求?我试过了:
xmlhttp.open("GET","www.example.com/example.php?d=a",true);
xmlhttp.send();
有人可以为我提供一个工作示例吗?谢谢。
我如何在javascript中发出像www.example.com/example.php?d=a这样的ajax get请求?我试过了:
xmlhttp.open("GET","www.example.com/example.php?d=a",true);
xmlhttp.send();
有人可以为我提供一个工作示例吗?谢谢。
如果你真的想使用 GET,这里是代码:
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","the_page_to_send_request_to.someFileFormat",true);
xmlhttp.send();
    当然 :)
str此函数将使用您喜欢的任何数据 ( ) 向您喜欢的任何位置( ) 发出 (POST) 请求phplocation。这xmlhttp.responseText是服务器发回的任何内容:)
<script type="text/javascript">
function submitForm(str, phpLocation)
{
  var xmlhttp;
  if (window.XMLHttpRequest)
    xmlhttp=new XMLHttpRequest();
  else
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.onreadystatechange=function()
  {
    if(xmlhttp.readyState<4)
    {
      //i'm not ready yet. Do stuff.....
    }
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      //Now I'm ready. Do stuff with this: xmlhttp.responseText;
    }
  }
  xmlhttp.open("POST", phpLocation, true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send("str=" + encodeURIComponent(str));
}
</script>