ajax
一、定义
AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML;简短地说,在不重载整个网页的情况下,AJAX 通过后台加载数据,并在网页上进行显示
$("#id").load()
load(url,data,function(response,status,xhr))
$("button").click(function(){ $("div").load('demo_ajax_load.txt'); });
$.get();
$.get("http://datainfo.duapp.com/shopdata/getclass.php",function(data){ console.log(data) })
$.get("http://datainfo.duapp.com/shopdata/getuser.php?userID=f66",function(data){ console.log(data) },"JSONP"); $.get("http://datainfo.duapp.com/shopdata/getCar.php",{userID:"f66"},function(data){ console.log(data) },"JSONP")
$.post(URL,data,callback);
$.post("http://datainfo.duapp.com/shopdata/getuser.php?userID=f66",function(data){ console.log(data) },"JSONP"); $.post("http://datainfo.duapp.com/shopdata/getCar.php",{userID:"f66"},function(data){ console.log(data) },"JSONP")
$.ajax();
$.ajax({ type:"get", url:"", function(data){ console.log(data) } });
$.getJSON();
$.getJSON("http://datainfo.duapp.com/shopdata/getCar.php?userID=f66&callback=?",function(data){ console.log(data) }) $.getJSON("pro.json",function(data){
console.log(data) sortPrice(data); data.sort(function(a,b){ return a.price - b.price; }) console.log(data) })
$.getScript();
$.getScript("test.js",function(){ setTimeout(function(){ alert(2) },2000) })
用法区分
ajax 使用举例
三、扩展
1. 请求出错时 识别状态 打印状态码
$.ajax({
url: "theme1.txt",
type: "POST",
data: {
},
dataType: "text",
success: function (xml, textStatus, xhr) {
console.log(xml);
console.log(textStatus);
console.log(xhr);
},
// error:function(xml, textStatus, xhr){
// console.log('error');
// console.log(xml);
// console.log(textStatus);
// console.log(xhr);
// },
complete: function (xhr, textStatus) {
console.log('complete');
console.log(xhr.status);
console.log(textStatus);
}
});
Put the favorites in the favorites and empty the favorites.