jQuery ajax應(yīng)用總結(jié)
一、jQuery中Ajax的調(diào)用(需要引用jQuery代碼庫)。
方法1:
//deal with the data
});
方法2:
$.post(url,postdata, function(data) {
//deal with the data
});
方法3:
type: "POST",// or get
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) {
//deal with the data
},
error: function() {
// deal with error
}
});
二、jQuery.Form plugin Ajax(需要引用jQuery代碼庫和jQuery.Form插件)
基于Form表單的Ajax調(diào)用
1.ajaxForm, 這個方法在調(diào)用時不是馬上提交,只是說明調(diào)用的Form要以ajax方式提交,該方法一般在$(document).ready方法里設(shè)置。
2.ajaxSubmit,這個方法在調(diào)用時就會馬上提交。
target: '#divToUpdate',
url: 'comment.php',
success: function() {
alert('Thanks for your comment!');
}
};
$('#myForm').ajaxForm(options);
或$('#myForm').ajaxSubmit(options);
三、Ajax在MVC中的使用
以上兩種方法都可以用,
另外我們可以MicrosoftAjax,這就必須引用MicrosoftAjax.js, MicorsoftMvcAjax.js這兩個文件
1.Ajax.BeginForm
{
UpdateTargetId = "ajaxdiv",
HttpMethod = "POST"
}, new { id = "AjaxForm" }))
{ %>
<input type="text" id="EmployeeId2" />
<input type="submit" value="Submit" />
<%} %>
<div id="ajaxdiv">
</div>
2.Ajax.ActionLink
<%=Ajax.ActionLink("LinkName","action", "controll", new AjaxOptions
{
LoadingElementId = "loadingdiv",
UpdateTargetId = "ajaxdiv",
HttpMethod = "POST"
});%>
<div id="ajaxdiv">
</div>
<div id="loadingdiv">
</div>
四、jquery.form與jquery.validate結(jié)合使用
前端代碼
<script type="text/javascript" language="javascript" src="http://chabaoo.cn/Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript" language="javascript" src="http://chabaoo.cn/Scripts/jquery.validate.min.js"></script> <script type="text/javascript" language="javascript" src="http://chabaoo.cn/Scripts/jquery.form.js"></script> <h2> AjaxFrom</h2> <div id="output1" style="color: Red;"> </div> <%using (Html.BeginForm("Login", "Home", FormMethod.Post, new { id = "loginForm" })) { %> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> <%=Html.TextBox("UserEmail", "", new { @class="name required"})%> </td> </tr> <tr> <td> <%=Html.Password("Password", "", new { @class = "required" })%> </td> </tr> <tr> <td> <input type="submit" value="submit" /> </td> </tr> </table> <%} %> <script language="javascript" type="text/javascript"> $(document).ready(function () { var opts = { submitHandler: function (form) { var submitOpts = { target: '#output1', success: function () { alert('Thanks for your comment!'); } }; $(form).ajaxSubmit(submitOpts); } }; jQuery("#loginForm").validate(opts); }); </script>
后端Action
public PartialViewResult Login(string UserEmail, string Password) { // you code return PartialView("Success"); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- javascript jQuery $.post $.ajax用法
- 基于JQuery框架的AJAX實例代碼
- jquery ajax提交表單數(shù)據(jù)的兩種方式
- JQuery AJAX提交中文亂碼的解決方案
- Jquery ajaxsubmit上傳圖片實現(xiàn)代碼
- 用jQuery中的ajax分頁實現(xiàn)代碼
- 基于jquery的$.ajax async使用
- jquery的ajax()函數(shù)傳值中文亂碼解決方法介紹
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法總結(jié)
- jquery實現(xiàn)ajax提交form表單的方法總結(jié)
- MVC+jQuery.Ajax異步實現(xiàn)增刪改查和分頁
- jquery的ajax提交form表單的兩種方法小結(jié)(推薦)
相關(guān)文章
解析頁面加載與js函數(shù)的執(zhí)行 onload or ready
這篇文章主要介紹了頁面加載與js函數(shù)的執(zhí)行 onload or ready 需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12關(guān)于Jquery中的bind(),on()綁定事件方式總結(jié)
下面小編就為大家?guī)硪黄P(guān)于Jquery中的bind(),on()綁定事件方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10jQuery過濾特殊字符及JS字符串轉(zhuǎn)為數(shù)字
這篇文章主要介紹了jQuery過濾特殊字符及JS字符串轉(zhuǎn)為數(shù)字 的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-05-05jQuery常用數(shù)據(jù)處理方法小結(jié)
這篇文章主要介紹了jQuery常用數(shù)據(jù)處理方法,實例總結(jié)了trim、param、isArray、isFunction、each等jQuery常用的數(shù)據(jù)處理方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02