1

我买了一本书,开始学习如何使用 AJAX,有人给了我一个练习任务,我必须在一个 div 中输入值,然后向她给我的网站执行 AJAX 获取请求,以便在正确的 div 中接收信息。也许我在这本书之后错误地执行了 AJAX 获取请求。如果有人能指出我正确的方向,我将不胜感激。这不适合学校,我没有网络脚本语言的经验,但愿意学习。看看我的javascript,也许你可以帮助我。我还不确定在收到信息后如何显示信息,但我确定我不想在点击提交按钮时加载另一个页面!

此外,我有一种奇怪的方式来排列我用 C++ 编程的代码大约 4 年了。我已经随着时间的推移开发了它,如果很难理解,我很抱歉。如果是这样,那么我可以将其格式化并重新发布。另外,我正在尝试学习如何使用 AJAX GET 方法,而不是 POST 方法。我想以 JSON 格式返回信息。

<html>

    <head>

            <style type="text/css">
                    #header {
                            text-align: left;
                    }
                    #wrapper {

                            margin:bottom;
                            width:100%;

                    }

                    #sub-left {
                            float:left;
                width:225px;
            height:215px;
            border:1px solid black; 
            position: relative;
            text-align: left;
                    }
                    #sub-right {
            padding-left: 52px;
                            float:left;
            width:60%;
            height:45%;
            border:1px solid black; 
            position: relative;
            text-align: left;

                    }
        #sub-leftmost {

                            float:left;
            width:10%;
            height:100%;
            position: relative;
            text-align: left;
                    }

            </style>


    <script type=”text/javascript”&gt;
    // function create GetXmlHttpObject
    function GetXmlHttpObject(){
    if (window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
    }
    if (window.ActiveXObject){
    // code for IE6, IE5
    return new ActiveXObject(“Microsoft.XMLHTTP”);
    }
    return null;
    }

    function submitFormWithAjax(){
    var myAjaxGetrequest = new GetXmlHttpObject();

    var t2lName=document.testForm.namebox.value;
    var t2lEmail=document.testForm.ebox.value;
    var t2lAddress=document.testForm.addbox.value;
    var t2lPhone=document.testForm.phnbox.value;

    var parameters = "name=" + encodeURIComponent(t2lName) 
           + "&email=" +encodeURIComponent(t2lEmail)
           + "&address=" + encodeURIComponent(t2lAddress)
           + "&phone=" +encodeURIComponent(t2lPhone);

    myAjaxGetrequest.open("GET", "websitetosendandgetfrom.com" + parameters, true);
    myAjaxGetrequest.send( );

    if (myAjaxGetrequest.readyState==4){
    if(myAjaxGetrequest.status==200 || window.location.href.indexOf("http")==-1){
    document.getElementById("result").innerHTML=myAjaxGetrequest.responseText
    document.getElementById(“testForm”).style.display = “none”;
    }
    else    {
    document.getElementById(“testForm”).innerHTML=”An error has occured making the request”;
    }
    }
    }
    }   



</script>
    </head>

    <body>

<div id="wrapper">

    <div id="sub-leftmost">


    </div>
</div>


    <div id="wrapper">
    <div id="header"><h1>Quiz</h1></div>
    <div id="sub-left">
<form name = 'testForm'>
<FONT COLOR="CC3300",font size="5">&nbsp;&nbsp;&nbsp;<b>User Info</b></FONT>
<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Full Name: <br /><center><input type="text"  size="25" id =         "namebox" /></center>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email Address: <br /><center><input type="text"  size="25" id = "ebox" /></center>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address: <br /><center><input type="text"  size="25" id = "addbox" /></center>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Phone Number: <br /><center><input type="text"  size="25" id = "phnbox" />
<a href=”#” onclick=”submitFormWithAjax();”&gt;Finished!</a>
</form>

    </div>
    </div>
<div id="wrapper">
    <div id="sub-right">

    </div>
</div>


    </body>

    </html>
4

1 回答 1

1

The request is asynchronous and will only call a callback function when its status changes.

Basically, all the code that depends upon the request's status change needs to be wrapped into the callback of the AJAX request:

myAjaxGetrequest.send();

myAjaxGetrequest.onreadystatechange = function() {
  if (myAjaxGetrequest.readyState==4){
    ...
于 2012-07-11T23:58:18.977 回答