zoukankan      html  css  js  c++  java
  • 原生Ajax

    原生Ajax

    <span>输入账号 :</span>
    <input id="name" name="name" onkeyup="check()" type="text"> 
    <span id="checkResult"></span>
      
    <script>

    第一步:jsp中设置全局变量
    var xmlhttp;
    function check(){
      var name = document.getElementById("name").value;
      var url = "http://how2j.cn/study/checkName.jsp?name="+name;

      第二步:实例化XMLHttpRequest
      xmlhttp =new XMLHttpRequest();

      第三步:设置响应函数checkResult,注意checkResult()错误!:后面不能加括号,加括号表示函数的结果
      xmlhttp.onreadystatechange=checkResult; 

      第四部:设置访问的页面
      xmlhttp.open("GET",url,true);   //设置访问的页面

      第五部:执行访问
      xmlhttp.send(null);  //执行访问
    }
      
    function checkResult(){
      第六部:编写响应函数
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        document.getElementById('checkResult').innerHTML=xmlhttp.responseText;
       
    }
    </script>

    xmlhttp.send(null)
    null表示没有参数,因为参数已经通过“GET" 方式,放在url里了。
    只有在用"POST",并且需要发送参数的时候,才会使用到send。
    类似这样:
        在提交之前需要设置请求头
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send("user="+username+"&password="+password)

    xmlhttp.readyState 4 表示请求已完成
    xmlhttp.status 200 表示响应成功
    xmlhttp.responseText; 用于获取服务端传回来的文本
    服务端传文本不要使用println(),可以使用print();

    创建ajax对象有时候需要判定:
    var xmlHttp;
    if(window.ActiveXObject){
        xmlHttp = 
    }else if(window.XMLHttpRequest){
        xmlhttp =new XMLHttpRequest();
    }

  • 相关阅读:
    记一次for update“同一事务”中update无法获取数据锁的解决
    MD(markdown)文件的编写说明
    git简单使用总结
    各种集合、对象初始创建默认大小
    Java 5-11新特性的整理(转)
    Junit4学习使用和总结
    Java 8 Lambda表达式学习和理解
    java 防止sql注入的方法(非原创)
    介绍几个移动web app开发框架
    jQuery UI 之 EasyUI 快速入门
  • 原文地址:https://www.cnblogs.com/snzd9958/p/10098985.html
Copyright © 2011-2022 走看看