Domanda

This script works on one side but doesn't on the other. It gets an Uncaught TypeError: Cannot call method 'open' of undefined. The script is placed at the same spot on both sides...

This code is used to send a rating to the db 1-5 depending on which the user choose.

function send_ajax(bewertung, click) {
    if (click) {
        alt = userrating[{TOPIC_ID}];
        userrating[{TOPIC_ID}] = bewertung;
        xmlhttp.open("GET", "bewertung.php?{BEWERTUNGSVAR}&wertung=" + bewertung, true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = function () {
            var a = new Date();
            a = new Date(a.getTime() + 1000 * 60 * 60 * 24 * 365);
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                if (xmlhttp.responseText) {
                    document.getElementById("ratingdesc").innerHTML = "{L_RATINGCOUNTED}";
                } else {
                    userrating[{TOPIC_ID}] = alt;
                    document.getElementById("ratingdesc").innerHTML = "Bewertung fehlgeschlagen... bitte nochmals versuchen!";
                }
            }
        }
    } else {
        if (bewertung) {
            document.getElementById("ratingstarbg").style.backgroundPosition = "0 -" + (bewertung * 17) + "px";
        } else {
            if (typeof userrating[{TOPIC_ID}] == 'undefined') {
                document.getElementById("ratingstarbg").style.backgroundPosition = "0 -{STARTBGPOS}px";
            } else {
                document.getElementById("ratingstarbg").style.backgroundPosition = "0 -" + (userrating[{TOPIC_ID}] * 17) + "px";
            }
        }
    }
}

and this is how it is called in the HTML

<div style="float:left">
<span id="ratingstarbg" class="star" style="background-position: 0 -{STARTBGPOS}px">
<a class="ratestar" href="javascript:send_ajax(1,true);" onmouseover="javascript:send_ajax(1,false);" onmouseout="javascript:send_ajax(0,false);"></a>
<a class="ratestar" href="javascript:send_ajax(2,true);" onmouseover="javascript:send_ajax(2,false);" onmouseout="javascript:send_ajax(0,false);"></a>
<a class="ratestar" href="javascript:send_ajax(3,true);" onmouseover="javascript:send_ajax(3,false);" onmouseout="javascript:send_ajax(0,false);"></a>
<a class="ratestar" href="javascript:send_ajax(4,true);" onmouseover="javascript:send_ajax(4,false);" onmouseout="javascript:send_ajax(0,false);"></a>
<a class="ratestar" href="javascript:send_ajax(5,true);" onmouseover="javascript:send_ajax(5,false);" onmouseout="javascript:send_ajax(0,false);"></a>
</span>
</div>
È stato utile?

Soluzione

Perhaps you are not instatiating a XMLHttpRequest object in the xmlhttp variable. You can try to put

var xmlhttp = new XMLHttpRequest();

Inside the function.

If you need compatibility with IE5 and IE6:

var xmlhttp;
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top