ajax请求和ajax提交表单
1、ajax
1、引入js(略)
2、调用$.ajax()方法取得数据
data可以使用
$("#myForm").serialize();
注意:当使用表格布局时,如果使用ajax刷新出现无来由的多出一列的时候,可以尝试去除结束标记和开始标记之前的空格,参考链接:
http://stackoverflow.com/questions/7267014/ie9-table-has-random-rows-which-are-offset-at-random-columns
示例:
1
2
3
4
5
|
success:function(result){
result = result.replace(/>\s+(?=<\/?(t|c)[hardfob])/gm,
'>'
);
$(
"#main"
).empty();
$(
"#main"
).html(result);
}
|
调用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
27
28
29
|
$.ajax({
type:
"POST"
,
url:
""
,
dataType:
"json"
,
data:{
"test1"
:test1,
"test2"
:test2
},
beforeSend:function(){
//在执行action之前调用
},
async:
true
,
//true为同步,false为异步
success: function(result, textStatus){
if
(result.code==
"00"
){
alert( "
成功
"
);
}
else
{
alert(
"
失败
"
);
}
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(
"
失败
"
);
}
});
|
2、ajaxSubmit
1
2
|
<script type=
"text/javascript"
src=
"${ctx}/js/jquery.form.js"
></script>
<script src=
"${ctx}/js/jquery/jquery-1.8.3.min.js"
type=
"text/javascript"
></script>
|
2、调用ajaxSubmit
1
2
3
4
5
6
7
8
9
10
11
12
|
$(
"#myForm"
).ajaxSubmit({
success:function(data){
if
(data.code ==
'00'
){
window.location.href=window.location.href;
}
else
if
(data.code ==
'01'
){
$(
"#btn"
).alert(
"提交失败,请稍候再试"
);
}
},
error : function(){
$(
"#btn"
).alert(
"提交失败,请稍候再试"
);
}
});
|