mirror of
https://github.com/Estom/notes.git
synced 2026-02-13 07:16:13 +08:00
JavaScript&nodejs
This commit is contained in:
61
JavaScript/jQuery/6ajax.md
Normal file
61
JavaScript/jQuery/6ajax.md
Normal file
@@ -0,0 +1,61 @@
|
||||
## 1 load
|
||||
|
||||
### load方法
|
||||
jQuery load() 方法是简单但强大的 AJAX 方法。
|
||||
|
||||
load() 方法从服务器加载数据,并把返回的数据放入被选元素中
|
||||
|
||||
```
|
||||
$(selector).load(URL,data,callback);
|
||||
|
||||
$("#div1").load("demo_test.txt");
|
||||
```
|
||||
|
||||
### load回调参数
|
||||
|
||||
可选的 callback 参数规定当 load() 方法完成后所要允许的回调函数。回调函数可以设置不同的参数:
|
||||
|
||||
responseTxt - 包含调用成功时的结果内容
|
||||
statusTXT - 包含调用的状态
|
||||
xhr - 包含 XMLHttpRequest 对象
|
||||
|
||||
```
|
||||
$("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 GET方法
|
||||
GET - 从指定的资源请求数据.GET 基本上用于从服务器获得(取回)数据。注释:GET 方法可能返回缓存数据。
|
||||
```
|
||||
$.get(URL,callback);
|
||||
|
||||
$("button").click(function(){
|
||||
$.get("demo_test.php",function(data,status){
|
||||
alert("数据: " + data + "\n状态: " + status);
|
||||
});
|
||||
});
|
||||
```
|
||||
## 3 POST方法
|
||||
|
||||
POST - 向指定的资源提交要处理的数据.POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据
|
||||
|
||||
```
|
||||
$.post(URL,data,callback);
|
||||
|
||||
$("button").click(function(){
|
||||
$.post("/try/ajax/demo_test_post.php",
|
||||
{
|
||||
name:"菜鸟教程",
|
||||
url:"http://www.runoob.com"
|
||||
},
|
||||
function(data,status){
|
||||
alert("数据: \n" + data + "\n状态: " + status);
|
||||
});
|
||||
});
|
||||
```
|
||||
Reference in New Issue
Block a user