ajax有问题



 <script type="text/javascript">
function ajax()
{
var xmlhttp=new XMLHttpRequest();

 xmlhttp.timeout = 3000;
 xmlhttp.onreadystatechange=function()
 {
    if (xmlhttp.readyState==4&&xmlhttp.status==200)
    {
        document.write("成功连接服务器");
        if (xmlhttp.readyState==4&&xmlhttp.status==200&&xmlhttp.responseText==1)
    {
        alert("success!");
    }
        if (xmlhttp.readyState==4&&xmlhttp.status==200&&xmlhttp.responseText==0)
    {
        alert("wrong");
    }
    }
 }
   xmlhttp.ontimeout = function(event){
    alert('请求超时!');}
xmlhttp.open("GET","login.php",true);
xmlhttp.send();
}
</script>


 <input type="submit" text="提交" onclick="ajax()">

不知道为什么,点了按钮后没有实现js功能
求大神帮忙看一下有没有问题。。。

Ajax JavaScript

Alkiad 8 years, 9 months ago

对于有域限制的 XMLHttpRequest , 你第一步要做的是搭建好HTTP服务器,并在 http(s):// 协议下访问资源

永遠D魔法師 answered 8 years, 9 months ago

代码没问题,实测有结果。
1.login.php文件是不是存在
2.xmlhttp是不是被禁用,在自己电脑一般没问题,但有的空间商会禁用

请叫我恐鹊 answered 8 years, 9 months ago

请使用 Google Chrome DevTools

AsuraKK answered 8 years, 9 months ago

阻止一下默认函数吧,或者 <input type="submit" text="提交" onclick="ajax()"> 换成


 <a href='javascript:void(0)' onclick="ajax()">提交</a>

stanes answered 8 years, 9 months ago

首先需要确认你的请求有没有发送出去,用firebug/chrome开发者工具/fiddler等工具检测一下。
然后再找问题。

Flaya answered 8 years, 9 months ago

<input type="submit" text="提交" onclick="ajax()"> 换成普通按钮试试

红茶学长233 answered 8 years, 9 months ago

Your Answer