0

我正在使用 ajax(效果很好)来传递价值。但是当我添加 HTTP 代码时,没有任何动作。使用简单的 HTTP 来显示div基于http.readystatus. 这是正确的格式吗?如果不是,那是什么?

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);    
            }
        });
    }
}

任何帮助将不胜感激!提前致谢。

4

1 回答 1

2

Vanilla JS 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.
于 2013-04-25T14:21:56.257 回答