dongzun9958 2018-10-27 08:05
浏览 162

AJAX问题处理响应数据用于显示

I'm working on a project at the moment and was trying to use some AJAX to make submitting some smaller forms more smooth. I tried testing it by getting the "submit" button to just display text from a .txt file in a paragraph underneath it but that didn't work...

Here's my code:

<div style="color: #818181; width: auto; height 50vh; display:flex; flex-direction: row; justify-content: space-around;">

<div style="padding: 2vw; width: 35vw; height: 50vh; display: flex; justify-content: top; flex-direction: column; ">
<h2 style="color: #818181; margin: 0;">Contact</h2>
Questions? Go ahead.
<form method="post">
<p><input name='lcontactname' type="text" placeholder="Name" style="height: 5vh; width:28vw" required></p>
<p><input name='lcontactemail' type="text" placeholder="Email" style="height: 5vh; width:28vw" required></p>
<p><input name='lcontactsubject' type="text" placeholder="Subject" style="height: 5vh; width:28vw" required></p>
<p><input name='lcontactmessage' type="text" placeholder="Message" style="height: 5vh; width:28vw" required></p>
<p><input onClick='submitlContact' type='button' value='send' style="height: 5vh; width: 28vw; color: white;background-color:black; border: 2px white; padding: auto; font-size: calc(1vw + 0.5vh);" onClick='lcontactSend();'></p>
<p id='testpar'></p>
</form>
</div>

<script>
function submitlContact(){ //AJAX TO SUBMIT THIS FORM
    //if (window.XMLHttpRequest) {
    // code for modern browsers
    xhttp = new XMLHttpRequest();
    //} else {
    // code for IE6, IE5
   // xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    //}
    xhttp.open("GET", "lcontactsubmit.txt", true);
    xhttp.send();
    document.getElementById("testpar").innerHTML = xhttp.responseText;

}   



</script>

The idea is that when the button is pressed I can see the contents of the .txt file which has a random word in called lcontactsubmit.txt and definitely is saved to contain a random jumble of letters.

Any suggestions?

:)

  • 写回答

1条回答 默认 最新

  • douji6461 2018-10-27 09:40
    关注

    you have to wait for responce first, the xhttp.responseText is not available immediately after you send request, to do it use some event, for example onreadystatechange

    like:

    xhttp = new XMLHttpRequest();
    xhttp.open("GET", "lcontactsubmit.txt", true);
    xhttp.onreadystatechange = function() {
        if (this.readyState==4 && this.status==200) {
            document.getElementById("testpar").innerHTML = this.responseText;
        }
    }
    xhttp.send();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误