weixin_33696106
weixin_33696106
2012-09-24 13:05
采纳率: 0%
浏览 24

无法计算出ajax responsetext

I am new to web development and am trying to write simple code that changes visibility of an HTML element using JavaScript and AJAX, but my code is not working.

The PHP script I'm running is simple:

    <?php
     echo TRUE;
    ?>

The JavaScript code is:

function getuname() {
  //create_xmlhttpobj();
  var userName = document.getElementById('uName');

  var url = "http://localhost/test.php";
  //script that returns whether username exisits or not

  request.open("GET", url, true);
  request.send(null);
  request.onreadystatechange = updatepage();
  //alert(userName.value);
}

function updatepage() {
  //alert(request.readyState);
  alert(request.readyState);
  //alert(request.responseText);
  if (request.readyState == 4) {
    alert('here123');
    togvis();
  }
}

The statement alert(request.responseText); shows a blank alert box.

Can someone tell me what I might be doing wrong?

图片转代码服务由CSDN问答提供 功能建议

                    

我是Web开发的新手,正在尝试编写简单的代码来使用JavaScript和AJAX更改HTML元素的可见性,但是我的代码无法正常工作。

我正在运行的PHP脚本很简单:

  <?php
      echo TRUE;
     ?>
  

 

JavaScript代码为:

  function getuname(){
   // create_xmlhttpobj();
   var userName = document.getElementById('uName');

   var url =“ http://localhost/test.php”;
   //返回用户名是否存在的脚本

   request.open(“ GET”,url,true);
   request.send(null);
   request.onreadystatechange = updatepage();
   //alert(userName.value);
 }

 函数updatepage(){
   //alert(request.readyState);
   alert(request.readyState);
   //alert(request.responseText);
   如果(request.readyState == 4){
     alert('here123');
     togvis();
   }
 }
  

 

语句 alert(request.responseText); 显示空白警报框。

有人可以告诉我我可能在做错什么吗?     

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • weixin_33713707
    weixin_33713707 2012-09-24 13:05

    You are calling the function updatepage, not assigning it.

    request.onreadystatechange = updatepage();
    

    needs to be

    request.onreadystatechange = updatepage;
    
    点赞 评论

相关推荐