jQuery初学5(AJAX)

  • load
    下面的例子会把文件 "demo_test.txt" 的内容加载到指定的 <div> 元素中:
    $("#div1").load("demo_test.txt");
    下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中:
    $("#div1").load("demo_test.txt #p1");
    可选的 callback 参数规定当 load() 方法完成后所要允许的回调函数。回调函数可以设置不同的参数:
    ----->responseTxt - 包含调用成功时的结果内容
    ----->statusTXT - 包含调用的状态
    ----->xhr - 包含 XMLHttpRequest 对象
    下面的例子会在 load() 方法完成后显示一个提示框。如果 load() 方法已成功,则显示"外部内容加载成功!",而如果失败,则显示错误消息:
    $("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);
      });
    });
    

      

  • get
    $.get() 方法通过 HTTP GET 请求从服务器上请求数据。
    $.get(URL,callback);
    ----->必需的 URL 参数规定您希望请求的 URL。
    ----->可选的 callback 参数是请求成功后所执行的函数名。
    下面的例子使用 $.get() 方法从服务器上的一个文件中取回数据:
     $("button").click(function(){
     $.get("demo_test.php",function(data,status){
        alert("数据: " + data + "\n状态: " + status);
      });
    });
    

      

  • post
    $.post() 方法通过 HTTP POST 请求向服务器提交数据。
    $.post(URL,data,callback);
    ----->必需的 URL 参数规定您希望请求的 URL。
    ----->可选的 data 参数规定连同请求发送的数据。
    ----->可选的 callback 参数是请求成功后所执行的函数名。
    下面的例子使用 $.post() 连同请求一起发送数据:
    $("button").click(function(){
        $.post("/try/ajax/demo_test_post.php",
        {
            name:"菜鸟教程",
            url:"http://www.runoob.com"
        },
            function(data,status){
            alert("数据: \n" + data + "\n状态: " + status);
        });
    });
    

      

猜你喜欢

转载自www.cnblogs.com/linnannan/p/9935401.html