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 に答える