使用 getElementById 获取类型错误


Getting Type Error with getElementById

我使用以下代码调用另一个脚本:

function savecoupon<?php echo $r['id']; ?>() {
    var listingid = document.savecouponform<?php echo $r['id']; ?>.listingid.value;
    http.open('get', 'script/save_coupon.php?listingid='+listingid+'');
    http.onreadystatechange = handleResponse;
    http.send(null);
}
function handleResponse() {
    if(http.readyState == 4){
        response = http.responseText;
        alert(response);
        document.getElementById(response).innerHTML = "Saved";
    }
}

alert(response)显示正确的结果,这是我要替换的div 的元素 id。 但是由于某种原因,当我将响应放入getElementById时,我在firebug中收到以下错误:

TypeError: document.getElementById(...) is null
document.getElementById(response).innerHTML = "Saved";

如果我将 innerHTML 命令替换为 document.getElementById("savecouponarea1327").innerHTML = "Saved";一切按预期工作。 为什么在getElementById中没有读取响应?

从基本调试

console.log(escape(response));

显示它有额外的字符:%0D%0Asavecouponarea1327

%0D是回车符。 %0A是换行符。

您的服务器端代码中有额外的行。