Files
notes_estom/JavaScript/jQuery/6ajax.md
2020-07-21 09:13:01 +08:00

1.6 KiB
Raw Blame History

1 load

load方法

jQuery load() 方法是简单但强大的 AJAX 方法。

load() 方法从服务器加载数据,并把返回的数据放入被选元素中

$(selector).load(URL,data,callback);

$("#div1").load("demo_test.txt");

load回调参数

可选的 callback 参数规定当 load() 方法完成后所要允许的回调函数。回调函数可以设置不同的参数:

responseTxt - 包含调用成功时的结果内容 statusTXT - 包含调用的状态 xhr - 包含 XMLHttpRequest 对象

$("button").click(function(){
  $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
    if(statusTxt=="success")
      alert("外部内容加载成功!");
    if(statusTxt=="error")
      alert("Error: "+xhr.status+": "+xhr.statusText);
  });
});

2 GET方法

GET - 从指定的资源请求数据.GET 基本上用于从服务器获得取回数据。注释GET 方法可能返回缓存数据。

$.get(URL,callback);

$("button").click(function(){
  $.get("demo_test.php",function(data,status){
    alert("数据: " + data + "\n状态: " + status);
  });
});

3 POST方法

POST - 向指定的资源提交要处理的数据.POST 也可用于从服务器获取数据。不过POST 方法不会缓存数据,并且常用于连同请求一起发送数据

$.post(URL,data,callback);

$("button").click(function(){
    $.post("/try/ajax/demo_test_post.php",
    {
        name:"菜鸟教程",
        url:"http://www.runoob.com"
    },
    function(data,status){
        alert("数据: \n" + data + "\n状态: " + status);
    });
});