jquery与服务器交换数据的利器--ajax(异步javascript and xml)

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

一、下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中:

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

参考网址: http://www.w3cschool.cc/jquery/jquery-ajax-load.html

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

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

下面的例子会在 load() 方法完成后显示一个提示框。如果 load() 方法已成功,则显示"外部内容加载成功!",而如果失败,则显示错误消息:

$("button").click(function(){
  $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
    if(statusTxt=="success")
      alert("External content loaded successfully!");
    if(statusTxt=="error")
      alert("Error: "+xhr.status+": "+xhr.statusText);
  });
});
原文地址:https://www.cnblogs.com/camelroyu/p/4172684.html