Firebug 给出错误“Ajax 未定义”

发布于 2024-11-25 12:47:13 字数 2210 浏览 0 评论 0原文

我有这个 HTML 表单,它使用 AJAX 将数据传递到 php 文件。 (我这样做是为了不必离开当前页面)。 Firebug 给了我两个错误:

document.getElementById("name") is null
ajaxget is not defined

我今天才开始使用 AJAX 是出于绝望,所以我对它还很陌生。 PHP 文件是 Second.php,重要的部分是它接收变量,以便它可以创建一个输出供另一个 html 稍后使用,但它不必响应:

        <html>
        <head>
        </head>

        <body>
        <script type="text/javascript">
         function ajaxRequest(){
         var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
         if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
          for (var i=0; i<activexmodes.length; i++){
           try{
            return new ActiveXObject(activexmodes[i])
           }
           catch(e){
            //suppress error
           }
          }
         }
         else if (window.XMLHttpRequest) // if Mozilla, Safari etc
          return new XMLHttpRequest()
         else
          return false
        }
          var mygetrequest=new ajaxRequest()
        mygetrequest.onreadystatechange=function(){
         if (mygetrequest.readyState==4){
          if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
           document.getElementById("result").innerHTML=mygetrequest.responseText
          }
          else{
           alert("An error has occured making the request")
          }
         }
        }
        var namevalue=encodeURIComponent(document.getElementById("name").value)
        var agevalue=encodeURIComponent(document.getElementById("age").value)
        mygetrequest.open("GET", "second.php?name="+namevalue+"&age="+agevalue, true)
        mygetrequest.send(null)

        </script>

        <form method="get" action="">
        Your name: <input type="text" id="name" name="name" size="25" /> <br />
        Your age: <input type="text" id="age" name="age" size="25" /> <br />
        <input type="button" value="submit" onClick="ajaxget()" />
        </form>
         <div id="result"> </div>

        </body>

        </html>

I have this HTML form which uses AJAX to pass data to a php file. (I did this so I don't have to leave the current page). Firebug gives me two errors :

document.getElementById("name") is null
ajaxget is not defined

I only started using AJAX today out of desperation, so I'm very new to it. The PHP file is second.php, the important part is that it receives the variables so it can create an output to be used by another html later, but it doesn't have to respond back:

        <html>
        <head>
        </head>

        <body>
        <script type="text/javascript">
         function ajaxRequest(){
         var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
         if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
          for (var i=0; i<activexmodes.length; i++){
           try{
            return new ActiveXObject(activexmodes[i])
           }
           catch(e){
            //suppress error
           }
          }
         }
         else if (window.XMLHttpRequest) // if Mozilla, Safari etc
          return new XMLHttpRequest()
         else
          return false
        }
          var mygetrequest=new ajaxRequest()
        mygetrequest.onreadystatechange=function(){
         if (mygetrequest.readyState==4){
          if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
           document.getElementById("result").innerHTML=mygetrequest.responseText
          }
          else{
           alert("An error has occured making the request")
          }
         }
        }
        var namevalue=encodeURIComponent(document.getElementById("name").value)
        var agevalue=encodeURIComponent(document.getElementById("age").value)
        mygetrequest.open("GET", "second.php?name="+namevalue+"&age="+agevalue, true)
        mygetrequest.send(null)

        </script>

        <form method="get" action="">
        Your name: <input type="text" id="name" name="name" size="25" /> <br />
        Your age: <input type="text" id="age" name="age" size="25" /> <br />
        <input type="button" value="submit" onClick="ajaxget()" />
        </form>
         <div id="result"> </div>

        </body>

        </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初雪 2024-12-02 12:47:13

document.getElementById("name") 为空

您在任何函数之外都有 var namevalue=encodeURIComponent(document.getElementById("name").value) 。因此,它在解析

ajaxget 未定义

您有 onClick="ajaxget()" 但没有任何地方提及 ajaxget (例如 function ajaxget() { …< /代码>

document.getElementById("name") is null

You have var namevalue=encodeURIComponent(document.getElementById("name").value) outside of any function. So it runs when the <script> element is parsed — which is before the element with that id exists in the DOM.

ajaxget is not defined

You have onClick="ajaxget()" but nowhere do you have any other mention of ajaxget (such as function ajaxget() { …

逐鹿 2024-12-02 12:47:13
    <input type="button" value="submit" onClick="ajaxget()" />
                                                 ^^^^^^^^^--- where is this function?

您的代码中没有定义 ajaxget() 函数。

    <input type="button" value="submit" onClick="ajaxget()" />
                                                 ^^^^^^^^^--- where is this function?

You do not have an ajaxget() function defined in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文