2010-10-18 181 views
1

我尝试了下面的代码,通过单击按钮向jsp页面发送请求。我检查了Httpfox,但没有要求。我只是在html代码的主体中使用了整个代码。我在做一些愚蠢的错误吗?请建议..XMLHTTP请求请求不起作用

<button type="button" onClick="handleButtonClick();">Click Me!</button> 
<script type="text/javascript"> 




function handleButtonClick() 
{ 
    // Declare the variables we'll be using 
    var xmlHttp, handleRequestStateChange; 

    // Define the function to be called when our AJAX request's state changes: 
    handleRequestStateChange = function() 
    { 
     // Check to see if this state change was "request complete", and 
     // there was no server error (404 Not Found, 500 Server Error, etc) 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      var substring=xmlHttp.responseText; 
      // Do something with the text here 
      alert(substring); 
     } 
    } 

    xmlhttp = new XMLHttpRequest(); 
    xmlHttp.open("GET", "http://csce:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true); 
    xmlHttp.onreadystatechange = handleRequestStateChange; 
    xmlHttp.send(null); 
} 
</script> 

回答

4

那么,在JavaScript中,变量区分大小写。你有xmlHttpxmlhttp;那些应该是一样的。

您在<script>块的开头也有<pre><code>,这是一个JavaScript语法错误。

+0

好搭档!完全错过了,当我检查它。这很可能是。 – 2010-10-18 04:28:03

+0

谢谢我没有意识到我正在使用不同的情况... – Judy 2010-10-18 04:49:32

1

由于没有请求被提出,我不相信,你实际上可以使请求的“http://欧安会:8080”火狐可能看不到网址为位于同一子(您不能对与请求者不在同一域中的资源发出Ajax请求)。

假设您使URL相对。请求是否会生成呢?如果是这样,那很可能是你的问题。

+0

检查tmont的回答首先,它可能是正确的。 – 2010-10-18 04:31:16

1

Quote:xmlhttp = new XMLHttpRequest();

两件事情。首先,您可能想要使用更强大的方法来获取XMLHttpRequest对象。其次,javascript区分大小写; !XMLHTTP = XMLHTTP

xmlHttp = (function (x,y,i) { 
    if (x) return new x(); 
    for (i=0; i<y.length; y++) try { 
     return new ActiveXObject(y[i]); 
    } catch (e) {} 
})(
    window.XMLHttpRequest, 
    ['Msxml2.XMLHTTP','Microsoft.XMLHTTP'] 
); 

报价:http://csce:8080/test/ind ...

记住,跨域XMLHTTP是禁止的。除非你从csce:8080提供服务,否则这是行不通的。