AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML)。ajax不是一种新的技术,而是一种由多种已有技术的组合(例如带橡皮的铅笔)html
jQuery 提供多个与 AJAX 有关的方法。经过 jQuery AJAX 方法,您可以使用 HTTP Get 和 HTTP Post 从远程服务器上请求文本、HTML、XML 或 JSON - 同时您可以把这些外部数据直接载入网页的被选元素中。ajax
能够理解为ajax是沟通前台和服务器的桥梁缓存
1加载函数 load()服务器
$(selector).load(URL,data,callback);
这里由三个函数,异步
url为请求的的 URL,必要参数函数
data 可选参数,与请求一同发送的查询字符串键/值对集合post
callback 回调函数名称url
$("#div1").load("demo_test.txt"); //ajax获取txt文档内容
load()函数等同于.htmlspa
$('#test').html("<h2>jQuery and AJAX is FUN!!!</h2><p id='p'>This is some text in a paragraph.</p>");
callback()回调函数code
$("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 HTTP请求 POST和GET
$.get(url,callback);
$.post(URL,data,callback);
必需的 URL 参数规定您但愿请求的 URL。
可选的 data 参数规定连同请求发送的数据。 //data为发送请求时向后台传送的数据
后台是这样接受数据和返回数据的
<% dim fname,city fname=Request.Form("name") city=Request.Form("city") Response.Write("Dear " & fname & ". ") Response.Write("Hope you live well in " & city & ".") %>
可选的 callback 参数是请求成功后所执行的函数名。
$("button").click(function(){ $.post("demo_test_post.asp", { name:"Donald Duck", city:"Duckburg" }, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }); });
关于POST和GET的两种方式请看下图