未捕获的TypeError:无法调用未定义的方法'open'

时间:2013-11-12 16:36:35

标签: javascript xhtml request rating

此脚本一方面工作,另一方面则不工作。它得到一个Uncaught TypeError:无法调用未定义的方法'open'。剧本放在两边的同一个地方......

此代码用于根据用户选择的内容向db 1-5发送评级。

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

这就是它在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>

1 个答案:

答案 0 :(得分:0)

也许您没有在xmlhttp变量中设置XMLHttpRequest对象。 你可以试着把

var xmlhttp = new XMLHttpRequest();

在函数内部。

如果您需要与IE5和IE6兼容:

var xmlhttp;
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}