zoukankan      html  css  js  c++  java
  • jQuery 中的 Ajax 方法(节选)

    $.ajax()

    基本用法:

    $.ajax({
        url: url,                      // 地址
        data: data,                    // 参数
        type: 'POST',                  // 提交方式 可以选择 post/get 推荐 post
        async: false,                  // 同步异步
        dataType: 'json',              // 返回数据类型
        success:function(data){
            accidentList = data.list;  // 这里可以直接取 data 里面的数据了,因为设置返回值为 json 方式
        }
    });

    举个例子:

    document.getElementById("search").onclick = function() { 
        var request = new XMLHttpRequest();
        request.open("GET", "server.php?number=" + document.getElementById("keyword").value);
        request.send();
        request.onreadystatechange = function() {
            if (request.readyState===4) {
                if (request.status===200) { 
                    document.getElementById("searchResult").innerHTML = request.responseText;
                } else {
                    alert("发生错误:" + request.status);
                }
            } 
        }
    }
    
    document.getElementById("save").onclick = function() { 
        var request = new XMLHttpRequest();
        request.open("POST", "server.php");
        var data = "name=" + document.getElementById("staffName").value 
                          + "&number=" + document.getElementById("staffNumber").value 
                          + "&sex=" + document.getElementById("staffSex").value 
                          + "&job=" + document.getElementById("staffJob").value;
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send(data);
        request.onreadystatechange = function() {
            if (request.readyState===4) {
                if (request.status===200) { 
                    document.getElementById("createResult").innerHTML = request.responseText;
                } else {
                    alert("发生错误:" + request.status);
                }
            } 
        }
    }

    用 jQuery 改写如下:

    $(document).ready(function(){ 
        $("#search").click(function(){ 
            $.ajax({ 
                type: "GET",     
                url: "http://127.0.0.1:8080/ajaxdemo/serverjson2.php?number=" + $("#keyword").val(),
                dataType: "json",
                success: function(data) {
                    if (data.success) { 
                        $("#searchResult").html(data.msg);
                    } else {
                        $("#searchResult").html("出现错误:" + data.msg);
                    }  
                },
                error: function(jqXHR){     
                   alert("发生错误:" + jqXHR.status);  
                },     
            });
        });
        
        $("#save").click(function(){ 
            $.ajax({ 
                type: "POST",     
                url: "serverjson.php",
                data: {
                    name: $("#staffName").val(), 
                    number: $("#staffNumber").val(), 
                    sex: $("#staffSex").val(), 
                    job: $("#staffJob").val()
                },
                dataType: "json",
                success: function(data){
                    if (data.success) { 
                        $("#createResult").html(data.msg);
                    } else {
                        $("#createResult").html("出现错误:" + data.msg);
                    }  
                },
                error: function(jqXHR){     
                   alert("发生错误:" + jqXHR.status);  
                },     
            });
        });
    });
  • 相关阅读:
    java之内部类与匿名内部类
    学习Android之内部类
    Android横竖屏切换总结
    java实现快速排序
    Palindrome Partitioning II
    Palindrome Partitioning
    Clone Graph
    Word Break
    Gas Station
    Candy
  • 原文地址:https://www.cnblogs.com/Leophen/p/11210469.html
Copyright © 2011-2022 走看看