jQuery ajax应用总结
一、jQuery中Ajax的调用(需要引用jQuery代码库)。
方法1:
$.get(url,function(data){
//dealwiththedata
});
方法2:
jQuery.post(url,[data],[success(data,textStatus,jqXHR)],[dataType])
$.post(url,postdata,function(data){
//dealwiththedata
});
方法3:
$.ajax({
type:"POST",//orget
contentType:"application/json;charset=utf-8",
url:url,
data:"{'countryModel':"+JSON.stringify(countryModel)+"}",
dataType:"json",//html,xml,script
async:true,//true表示异步,默认就是true
success:function(data){
//dealwiththedata
},
error:function(){
//dealwitherror
}
});
二、jQuery.FormpluginAjax(需要引用jQuery代码库和jQuery.Form插件)
基于Form表单的Ajax调用
1.ajaxForm,这个方法在调用时不是马上提交,只是说明调用的Form要以ajax方式提交,该方法一般在$(document).ready方法里设置。
2.ajaxSubmit,这个方法在调用时就会马上提交。
varoptions={
target: '#divToUpdate',
url: 'comment.php',
success: function(){
alert('Thanksforyourcomment!');
}
};
$('#myForm').ajaxForm(options);
或$('#myForm').ajaxSubmit(options);
三、Ajax在MVC中的使用
以上两种方法都可以用,
另外我们可以MicrosoftAjax,这就必须引用MicrosoftAjax.js,MicorsoftMvcAjax.js这两个文件
1.Ajax.BeginForm
<%using(Ajax.BeginForm("action","controll",newAjaxOptions
{
UpdateTargetId="ajaxdiv",
HttpMethod="POST"
},new{id="AjaxForm"}))
{%>
<inputtype="text"id="EmployeeId2"/>
<inputtype="submit"value="Submit"/>
<%}%>
<divid="ajaxdiv">
</div>
2.Ajax.ActionLink
<%=Ajax.ActionLink("LinkName","action","controll",newAjaxOptions
{
LoadingElementId="loadingdiv",
UpdateTargetId="ajaxdiv",
HttpMethod="POST"
});%>
<divid="ajaxdiv">
</div>
<divid="loadingdiv">
</div>
四、jquery.form与jquery.validate结合使用
前端代码
<scripttype="text/javascript"language="javascript"src="https://www.nhooo.com/Scripts/jquery-1.4.1.min.js"></script>
<scripttype="text/javascript"language="javascript"src="https://www.nhooo.com/Scripts/jquery.validate.min.js"></script>
<scripttype="text/javascript"language="javascript"src="https://www.nhooo.com/Scripts/jquery.form.js"></script>
<h2>
AjaxFrom</h2>
<divid="output1"style="color:Red;">
</div>
<%using(Html.BeginForm("Login","Home",FormMethod.Post,new{id="loginForm"}))
{%>
<tableborder="0"cellpadding="0"cellspacing="0">
<tr>
<td>
<%=Html.TextBox("UserEmail","",new{@class="namerequired"})%>
</td>
</tr>
<tr>
<td>
<%=Html.Password("Password","",new{@class="required"})%>
</td>
</tr>
<tr>
<td>
<inputtype="submit"value="submit"/>
</td>
</tr>
</table>
<%}%>
<scriptlanguage="javascript"type="text/javascript">
$(document).ready(function(){
varopts={
submitHandler:function(form){
varsubmitOpts={
target:'#output1',
success:function(){
alert('Thanksforyourcomment!');
}
};
$(form).ajaxSubmit(submitOpts);
}
};
jQuery("#loginForm").validate(opts);
});
</script>
后端Action
publicPartialViewResultLogin(stringUserEmail,stringPassword)
{
//youcode
returnPartialView("Success");
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。