编写此AJAX调用的最简单方法

时间:2010-11-06 09:06:16

标签: javascript html

我需要向网络服务器“http://examples.com/ajax”发送ajax请求,该响应将是<div>的html,并且会插入现有的<div id="holder"> 。在javascript中编写此内容的最简单,最简单的方法是什么?不使用jQuery?

它只需要支持最新版本的chrome。

1 个答案:

答案 0 :(得分:3)

var req = new XMLHttpRequest();

req.onreadystatechange = function() {
  //Is request finished? Does the requested page exist?
  if(req.readyState==4 && req.status==200) {   
    //Your HTML arrives here
    alert(req.responseText);  
  }
}

req.open("GET","http://examples.com/ajax.php",true)  //true indicates ASYNCHRONOUS
req.send(null);

此解决方案使用get,因此您必须使用?&向您的网址添加变量(例如http://examples.com/ajax.php?foo=bar&blah=blee

如果您想使用post执行此操作,请使用get然后this article is useful运行一些。