编写这个AJAX调用的最简单的方法 [英] Simplest way to write this AJAX call
问题描述
我需要发送ajax请求到网络服务器http://examples.com/ajax,响应将是< div>
的html和它将被插入到现有的< div id =持有人>
中。用javascript写这个最简单,最小的方法是什么?而不使用jQuery?
它只需要支持最新版本的chrome。
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
//请求是否完成?请求的页面是否存在?
if(req.readyState == 4&& req.status == 200){
//你的HTML到达这里
alert(req.responseText);
req.open(GET,http://examples.com/ajax.php,true)// true表示ASYNCHRONOUS
req.send(null);
这个解决方案使用 get
我们必须使用?
和&
来为您的网址添加变量(例如 http:/ /examples.com/ajax.php?foo=bar&blah=blee
。
如果您想使用 post
,用 get
然后运行这篇文章很有用。
I need to send an ajax request to a webserver "http://examples.com/ajax" the response will be the html of a <div>
and it will be inserted to an existing <div id="holder">
. What's the simplest, smallest way to write this in javascript? without using jQuery?
It only needs to support the latest version of chrome.
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);
This solution uses get
, so you've got to add variables using ?
and &
to your URL (e.g. http://examples.com/ajax.php?foo=bar&blah=blee
.
If you want to do it using post
, run a few with get
and then this article is useful.
这篇关于编写这个AJAX调用的最简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!