为什么console.log无法写入提示信息?

时间:2018-11-23 13:18:22

标签: javascript console

我的test.html,当单击ID为submit_botton的对象时,触发了snedData函数。

<input type="text" value="test">
<input id="submit_button" type="button" value="submit">
<p id="prompt">test</p>
<script>    
function sendData()
{
    console.log("i am log info");
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "write.php",true); 
    xmlHttp.send("q=test"); 
    xmlHttp.onreadystatechange = function(){        
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
        document.getElementById("prompt").innerHTML = xmlHttp.responseText;
        }
    }
}

ob = document.getElementById("submit_button"); 
ob.addEventListener("click",sendData);
</script>

test.php仅包含一行。

<?php
echo "haha";
?>

为什么console.log("i am log info ");无法激活,控制台中没有日志信息?
enter image description here

2 个答案:

答案 0 :(得分:0)

您忘记关闭脚本标签:</script>


编辑:

  

添加标签没有用

脚本标签是您的问题。现在,您已经添加了结束脚本标签,它可以工作了。我正在Chrome 70中进行测试。请点击下面的“运行”以在SO 70上进行测试。

如果它不能在您的代码库上运行,那么在您提供的示例代码之外的其他地方就会出现问题。

function sendData()
{
    console.log("i am log info");
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("post", "write.php",true);
    xmlHttp.send("q=test");
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
            document.getElementById("prompt").innerHTML = xmlHttp.responseText;
        }
    }
}

ob = document.getElementById("submit_button");
ob.addEventListener("click",sendData);
<input type="text" value="test">
<input id="submit_button" type="button" value="submit">
<p id="prompt">test</p>

答案 1 :(得分:0)

真正的原因是我的Chrome调试设置中的设置。 enter image description here

您可以在右上角看到隐藏的标签。
删除xml,现在会显示控制台信息。
enter image description here