zoukankan      html  css  js  c++  java
  • Ajax,纯Js+Jquery

    AJAX:Asynchronous Javascript and xml 异步,Js和Xml 交互式网页开发 不刷新页面,与服务器交互 详情请参照Jquery工具指南
    用在浏览器端的技术,无刷新,通过XmlHttpRequest访问页面
    纯js版----------

    if(XmlHttpRequest){ //判断是否支持XmlHttpRequest
    xhr= new XmlHttpRequest(); // 创建XmlHttpRequest对象
    }
    else{
    xhr= new Activexobject("Microsoft.XMLHTTP"); //不支持XmlHttpRequest,使用此方法创建 
    }
    xhr.open("get|post","url",true);
    xhr.send(); // 开始发送
    xhr.onreadystatechange= function(){ // 回调函数,当服务器将数据返回给浏览器触发方法
    if(xhr.readyState==4) //0没调用open方法,1表示未调用send方法,2正在等待状态码和头的返回,3 已接受部分数据,但还没接受完,不能使用该对象的属性和方法,4已加载,所有数据执行完毕
    if(xhr.status==200){ // 响应状态码,表示页面执行无误
    alert(xhr.responseText); // 输出接受到的文本
    }
    }
    

      


    发送post请求


    在send中写数据。并添加请求报头

    xhr.setRequestHeader("Content-Type",application/x-www-form-urlencoded)
    xhr.send("id=123&pwd=456");
    

      


    -------------------------------------
    Jquery版
    强大的Jquery。。只需要get页面就够了

    $.get("url",{"id" : "123","pwd":456},function(data)){ //自动把参数当做get请求传输
    alert(data)
    }
    

      


    只需要post页面。

    $.post("url",{"id" : 123,"pwd":456},function(data)){ //post请求
    alert(data)
    }
    

      


    //第三种写法

    $.ajax({
    type="get"| post,
    url="...",
    data:"参数",
    success:function(msg){...} // msg为从服务器接受到的数据
    })
    

      

    感谢着知识大爆炸的时代,感谢这人人共享的精神
  • 相关阅读:
    js的继承实现方式
    jdbc调用通用存储过程完成分页
    最近在忙些什么......
    【转】说服下属的“攻心术”
    设计模式原则详解
    【转】职场学做“功夫熊猫”
    内核初始化优化宏 ,初始化顺序, __init,__devexit等
    Linux 嵌入式启动以及优化
    每个程序员都该知道的10大编程格言
    linux 用户空间 和 内核空间 延时函数
  • 原文地址:https://www.cnblogs.com/Zhang-silence/p/6399151.html
Copyright © 2011-2022 走看看