doudun8705 2011-10-26 16:34
浏览 72
已采纳

基本的ajax逻辑

Upon suggestion of using Ajax for an html page, I decided to attempt to learn how it works. In my example, I'm just trying to get the response from a php file (which just echoes a simple string as a test) but it doesn't work, in that nothing actually happens.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <title> Incident Center </title>
   <!--<link rel="stylesheet" href="http://web.njit.edu/~swp5/assignment/style/style.css">-->
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <script type="text/javascript">
    function onsubmit()
    {
        var sender;
        if (window.XMLHttpRequest)
        {
            sender=new XMLHttpRequest();
        }
        else
        {
            sender=new ActiveXObject("Microsoft.XMLHTTP");
        }
        sender.onreadystatechange=function()
        {
            if (sender.readyState==4 && sender.status==200)
            {
                document.getElementById("myDiv").innerHTML=sender.responseText;
            }
        }
        sender.open("GET","proz.php",true);
        sender.send();
    }
    </script>
</head>
<body>
    <div class="header">
        Incident Center 
    </div>

            <p>
                <button onclick="onsubmit()">Test</button>
            </p>


    <div id="myDiv"></div>
</body>
</html>
  • 写回答

5条回答 默认 最新

  • dqkx69935 2011-10-26 17:04
    关注

    Using Opera 11.51 here, it doesn't actually like the fact that you use onsubmit() as a function name. Presumably because onsubmit() is actually already an eventhandler hook of a <button> element itself. I presume other browsers won't like this function name either.

    So, first off, rename the function. Let's assume dosubmit() here.

    Furthermore, you should wrap a button in a <form> element. And because a default button, acts as a submit button, the form is being submitted, causing the page to reload.

    To prevent this, you should let the function return false; and call the function like so onclick="return dosubmit()", if you are going to call it inline like this, or make the button a button of type button, like so: <button type="button" onclick="dosubmit()">

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭