Javascript Ajax调用httpstatusready=4不';不执行
我正在使用ajax——它工作得很好——来传递值。但是当我添加HTTP代码时,没有任何操作。基于Javascript Ajax调用httpstatusready=4不';不执行,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用ajax——它工作得很好——来传递值。但是当我添加HTTP代码时,没有任何操作。基于HTTP.readystatus使用简单HTTP显示不同的div值。这是正确的格式吗?如果没有,是什么 if (colorToCheck == gup("Player1")) { document.getElementById('win').innerHTML = player1 + " wins"; redScore += 1; //Browser Support Code
HTTP.readystatus
使用简单HTTP显示不同的div
值。这是正确的格式吗?如果没有,是什么
if (colorToCheck == gup("Player1")) {
document.getElementById('win').innerHTML = player1 + " wins";
redScore += 1;
//Browser Support Code
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");
}
if (xmlhttp.readyState == 3 && xmlhttp.status == 200) {
document.getElementById("save").innerHTML = "saving";
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//ajax call
var dataString = 'winner=' + player1 + '&player1=' + player1 + '&player2=' + player2 + '&matchNum=' + matchNum;
$.ajax({
type: "POST",
url: "red.php",
data:dataString,
cache: false,
success: function(response) {
$('.result13').html(response);
}
});
}
}
任何帮助都将不胜感激!提前感谢。AJAX调用的结构是:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","path/to/file.php"); // or "POST" if needed
xmlhttp.onreadystatechange = function() {
if( this.readyState == 3) document.getElementById('save').innerHTML = "saving";
if( this.readyState == 4) {
if( this.status != 200) alert("ERROR: Server returned "+this.status+" "+this.statusText);
else {
// do something
alert(this.responseText);
}
}
};
xmlhttp.send(data); // data is whatever POST data you want. Leave out if using GET.
为什么要混合使用这两种方法进行Ajax调用?你确定状态是200吗?@epascarello ya页面很好。。我还没有很好地掌握ajax。很抱歉,我是这方面的新手。谢谢回复。还有什么更好的格式呢?我的http必须使用ajax功能call@epascarello那么我可以把这个http状态调用放在ajax成功函数中吗?@lvaroG.Vicario好的,谢谢。。我会检查的。忘记XMLHTTPRequest,使用jQuery.ajaxJS非常流行,它是预先安装的。甚至jQuery也使用它我把
数据
放在最后一行上方的代码中。对不起,我为拿你开玩笑感到难过。。。“Vanilla JS”只是一个术语,用于表示普通的JavaScript,没有任何像jQuery、下划线或其他任何框架。我链接的网站是一个笑话,假装Vanilla JS是一种神奇的扩展,它已经存在于您的浏览器中,然后继续演示使用plan JavaScript比任何框架都要快。没关系,只要在调用xmlhttp.send
之前随时定义变量即可,然后,当调用xmlhttp.send
时,传递我写入数据的变量。另外,请确保在xmlhttp.open
调用中使用“POST”
,否则服务器将看不到它!