zoukankan      html  css  js  c++  java
  • ajax

    jq的ajax方法

    $.ajax({
       url:"/test.php",
       type:"post",
       async:true,            //是否异步
       data:{name:"张三",age:23},       //传入数据
       timeout:5000,         // 超时时间(毫秒)
       dataType:"jsonp",     //返回数据格式
       success:function(data,textStatus,jqXHR){
             console.log(data);  
       },         //成功执行函数
       error:function(){
             console.log("错误");
       },        // 请求失败时调用此函数
       beforesend:function(){
            
       },    //向服务器发送请求前执行一些动作
        complete:function(){
            
        }    //请求完成后回调函数 (请求成功或失败之后均调用)
        
    })          

    原生的ajax方法---get

    function getXMLHttp(){
        var xhr=null;
        if(window.XMLHttpRequest){
             xhr=new XMLHttpRequest();   //chrome ff
        }else{
             xhr=new ActiveXObject("Microsoft.XMLHttp");  //ie
        }
        return xhr;
    } window.onload
    =function () { var btn=document.getElementById("btn"); btn.onclick=function () { var xhr=getXMLHttp(); xhr.onreadystatechange=function () { if(xhr.readyState==4 && xhr.status==200){ document.getElementById("box").innerHTML=xhr.responseText; //处理返回数据 } } xhr.open("GET","get.php?name=xlj&age=24&tid="+Math.random(),true); xhr.send(); }
    }

    原生的ajax方法---post

    function getXMLHttp() {
        var xhr=null;
        if(window.XMLHttpRequest){
            xhr=new XMLHttpRequest();  //chrome ff
        }else{
            xhr=new ActiveXObject("Microsoft.XMLHttp");   //ie
        }
        return xhr;
    }
    var btn=document.getElementById("btn");
    btn.onclick=function () {
        var xhr=getXMLHttp();
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4 && xhr.status==200){
                document.getElementById("box").innerHTML=xhr.responseText;
            }
        }
        xhr.open("POST","post.php",true);
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xhr.send("name=xlj&age=25");
    }
  • 相关阅读:
    vue与laravel
    php artisan 命令
    HTTP 状态码
    PhpStorm提高效率的使用方法及设置
    好RESTful API的设计原则
    laravel 入门基础之安装
    c++ sizeof(字符数组)
    new delete/delete[] 测试
    linux g++ 查找动态链接库
    linux下定时器耗时研究
  • 原文地址:https://www.cnblogs.com/xlj-code/p/7903419.html
Copyright © 2011-2022 走看看