jquery中使用ajax的几种常用写法

2025-04-24 05:55:36
推荐回答(3个)
回答1:

  1. $.ajax({
     url: "test.html",
     cache: false,
     success: function(html){
       $("#results").append(html);
     }
    });

  2. $("#feeds").load("feeds.php", {limit: 25}, function(){
      alert("The last 25 entries in the feed have been loaded");
    });

  3. $.get("test.cgi", { name: "John", time: "2pm" },
     function(data){
       alert("Data Loaded: " + data);
     });

  4. $.post("test.php", { name: "John", time: "2pm" },
      function(data){
        alert("Data Loaded: " + data);
      });


回答2:

(jquery.object).load()、$.get()、$.post、$.ajax()

回答3:

第一种:
$.ajax({
type: "POST",
url: "ShowProduct.aspx/GetDHList",
data: '{CategoryId:"' + CategoryId + '"}',
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (data) {
var item = eval(data.d);
var StrHtml = "";
for (var i in item) {}
}, async: false
});
第二种:
$(document).ready(function(){
$("#b01").click(function(){
htmlobj=$.ajax({url:"/jquery/test1.txt",async:false});
$("#myDiv").html(htmlobj.responseText);
});
});

第三种:
$.ajax({ url: "test.html", context: document.body, success: function(){
$(this).addClass("done");
}});