jQuery ajax学习2
来源:互联网 发布:网络集成技术课后答案 编辑:程序博客网 时间:2024/05/17 04:54
1、jQuery ajax试试手
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html>
<head>
<title>
JQuery ajax应用
</title>
<script src=
"http://libs.baidu.com/jquery/1.10.2/jquery.min.js"
></script>
<script>
$(
function
(){
$(
"#btn1"
).click(
function
(){
// 点击事件里面发送一个ajax请求
$.ajax({
url :
"demo_test.txt"
,
//请求地址
success :
function
(result){
$(
"#p1"
).html(result);
//成功回调函数
}
});
});
});
</script>
</head>
<body>
<p id=
"p1"
>jQuery ajx应用</p>
<input type=
"button"
id=
"btn1"
value=
"点一下"
>
</body>
</html>
点击按钮,将p中的文件内容修改,通过ajax请求.
2、ajax函数解析
语法:
$.ajax({
name : value,
name : value,
name : value,
...
});
其中,参数如下图
3、实例解析
1)指定响应数据类型dataType
1
2
3
4
5
6
$(
"#btn2"
).click(
function
(){
$.ajax({
url :
"demo_ajax_script.js"
,
dataType :
"script"
});
});
2)各中状态下的回调函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$(
"#btn3"
).click(
function
(){
$.ajax({
url :
""
,
beforeSend :
function
(){
$(
"#div1"
).css({
"background-color"
:
"red"
,
"width"
:
"80px"
,
"height"
:
"80"
});
console.log(
"beforeSend"
);
},
complete :
function
(){
$(
"#div1"
).css({
"background-color"
:
"green"
,
"width"
:
"80px"
,
"height"
:
"80"
});
console.log(
"complete"
);
},
error :
function
(){
$(
"#div1"
).css({
"background-color"
:
"black"
,
"width"
:
"40px"
,
"height"
:
"40"
});
console.log(
"error"
);
}
});
});
得到console中的返回值
由此可以看出ajax调用的顺序
beforeSend --> success/error --> complete
无论成功与否,最终都会调用complete回调方法,相当于java中的finally
4、ajaxSetup()方法
用于设置ajax的参数
1
2
3
4
5
6
7
8
9
10
$(
"#btn1"
).click(
function
(){
// 点击事件里面发送一个ajax请求
$.ajaxSetup({
url :
"demo_test.txt"
,
//请求地址
success :
function
(result){
$(
"#p1"
).html(result);
//成功回调函数
}
});
$.ajax();
});
5、get()方法
发送一个get请求到指定url地址
参数
语法:$.get(URL,data,function(data,status,xhr),dataType)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html>
<head>
<title>
JQuery ajax应用
</title>
<script src=
"http://libs.baidu.com/jquery/1.10.2/jquery.min.js"
></script>
<script>
$(
function
(){
$(
"#btn1"
).click(
function
(){
$.get(
"/try/ajax/gethint.php?q="
+$(
"#txt1"
).val(),
//url
function
(data , status){
$(
"p"
).html(data);
}
//回调方法
);
});
});
</script>
</head>
<body>
<p>ajax 的Get和POST方法</p>
<input type=
"text"
id=
"txt1"
><br>
<input type=
"button"
id=
"btn1"
value=
"点一下"
>
<p></p>
</body>
</html>
$.post()方法
事例
1
2
3
4
5
6
7
8
//post方法
$.post(
"demo_ajax_gethint.php"
,
//url
{suggest : $(
"#txt1"
).val()},
//参数
function
(data, status){
$(
"p:eq(1)"
).html(data);
}
//回调函数
);
get()和post()方法均可以用ajax()代替
1
2
3
4
5
6
7
8
$.ajax({
type:
"POST"
,
url :
"demo_ajax_gethint.php"
,
data :
"suggest="
+ $(
"#txt1"
).val(),
success :
function
(data){
$(
"p:eq(1)"
).html(data);
}
});
0 0
- jQuery ajax学习2
- AJAX学习(2)jQuery中的Ajax
- JQuery的Ajax学习(2)
- Jquery学习--Jquery Ajax函数
- jQuery学习笔记--jQuery Ajax
- jQuery学习 五 jQuery Ajax
- ajax,jquery学习资源
- jquery ajax学习
- jQuery Ajax 函数学习
- jquery-ajax学习
- struts1 + jquery + ajax学习
- ajax(jQuery)学习笔记
- Jquery.ajax学习笔记
- jquery学习之AJAX
- Ajax及jQuery学习
- JQuery学习(5-AJAX)
- jquery学习 ajax一
- JQuery Ajax学习笔记
- PostgreSQL触发器
- mysql:慢查询的设置与分析
- MySQL分页的方法及优化
- MySQL 命令行导出及导入结构或数据
- dispatch_async
- jQuery ajax学习2
- oracle 12c for linux 6.6 installation
- Red hat Linux用yum命令安装MariaDB
- The script for Installing UHD+GNUradio
- vi / vim保存文件时遇到的问题:E212: Can't open file for writing
- 干货季01:vCenter Server安装到SSO的时候报错
- JDK源码(1)
- Codeforces-260A_Adding Digits
- 正则表达式