Jquery Ajax 总结如下:
[1] 原生 Ajax 对象 {Javascript}
//XMLHttpRequest 对象:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = 处理函数;
xhr.open("get","http://192.168....");
xhr.send( 数据 );
[2] load() 函数
如:
$("#box").load("http://....");
[3] post() 函数
以 post 方式发出请求, 在 Servlet 端会调用 doPost 来响应。
如:
$.post(
{id:xxxx},
//响应函数
);
[4] $.ajax() 函数
dataType:'json' 设置客户端接收数据格式
如果, 接收格式不对, 就会调用 error 函数
如果, 接收格式 OK, 就会调用 success 函数
dataType:'text/html' [默认格式]
如:
$.ajax(
{
url:'http://www.baidu.com/...',
type:'get', //请求类型
data:{username:'andy',password:'123'}, //发送服务端
dataType:'json',
success:function(resp, status, xhr){
//resp 则为客户端收到的数据。
},
error:function(exception){
//如果这个 error 被调用说明这个请求有问题。
}
}
);
[5] $.get() 函数
以 get 方式发出请求, 在 Servlet 端会调用 doGet 来响应。
function doTest(){
var url = "/data.txt";
$.get( url, onResponse );
}
function onResponse( respTxt, status, xhr ){
$("#box").text("收到消息:"+ respTxt);
}