日期:2014-05-19  浏览次数:20603 次

Java Web基础问题,XMLHTTPRequest使用问题
刚学XMLHTTPRequest的使用,试着发一个请求,然后输出响应的内容,但点击按钮后不输出响应的内容,求大神指教:急,QQ:907600323
<html>
  <head>
    <title>SetRequest.html</title>
<script type="text/javascript">
    function set(){ 
    alert("5555");
    var myRequest=null;
    if (window.XMLHttpRequest) { myRequest=new XMLHttpRequest() }
    else if (window.ActiveXObject) { myRequest=new ActiveXObject("Microsoft.XMLHTTP") } 
    if(myRequest){
       myRequest.open("GET","http://localhost:8080/webdemo/servlet/FirstServlethttp",false);
       myRequest.send(null);}
  }
  alert(myRequest.responseText);
  </script>  

  </head>
  
  <body>
    This is my HTML page. <br>
    <input type="button" value="发送请求" onclick="set()">
  </body>
</html>

------解决方案--------------------

<script language="javascript">
        var XMLHttpReq = false;
        
        //创建XMLHttpRequest对象
        function createXMLHttpRequest(){
         if(window.XMLHttpRequest){
         XMLHttpReq = new XMLHttpRequest();
         }else if(window.ActiveXobject){
         try{
         XMLHttpReq = new ActiveXobject("Msxm12.XMLHTTP");
         }catch(e){
         try{
         XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         }catch(e){}
         }
         }
        }
        //发送请求函数
        function sendRequest(url){
         createXMLHttpRequest();
         XMLHttpReq.open("GET",url,true);
         XMLHttpReq.onreadystatechange = processResponse;   //指定回调函数
         XMLHttpReq.send(null);   //发送请求
        }
        //处理返回信息函数
        function processResponse(){
         if(XMLHttpReq.readyState == 4){     //判断对象状态
         if(XMLHttpReq.status == 200){
        &nb