【发布时间】:2013-10-17 00:10:06
【问题描述】:
我一直在学习 AJAX,但我对 AJAX 调用中的方法执行的顺序有点困惑。我见过太多变种。例如
function submitArticle() {
try {
//alert("yaay");
xhr = new XMLHttpRequest();
}
catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
alert("Your Browser is not Supported");
return false;
}
}
}
var parameters = "postTitle=" + postTitle "&postDes=" + postDes + "&postCont=" + postDes;
xhr.open('POST', '/engine/engine.php', true);
xhr.send(parameters);
xhr.onreadystatechange = function() {
if(this.readyState === 4) {
if(this.status ===200) {
alert(this.responseText);
}
else {
alert("status" + this.status);
}
}
else {
alert("readyState" + this.readyState);
}
}
}
我的问题是,我看到代码中 open 和 send 方法放置在非常不同的位置,例如在评估 readyState 值之后。这是正确的方法。我在不同的网站上查找过,我看到的只是 jquery 教程,没有一个解释了代码的执行顺序。对不起,如果这是一个非常愚蠢的问题或者我的代码是错误的。
【问题讨论】:
-
该代码不正确。什么是
open()和send()。它缺少xhr. -
谢谢,我将对此进行更改。
标签: javascript ajax methods