ajax应用总结

Ajax在Web应用中的作用越来越大,许多工具都包含了对这一功能的使用,以下是对这些常用工具中Ajax的典型实例.

一、jQuery中Ajax的调用(需要引用jQuery代码库)。

  1.$.get(url, function(data) {
            //deal with the data
        });

  2.jQuery.post( url, [ data ], [ success(data, textStatus, jqXHR) ], [ dataType ] )

  $.post(url,postdata, function(data) {

    //deal with the data
  });
3.$.ajax({
        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插件)
  中文API地址:http://www.aqee.net/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started
  基于Form表单的Ajax调用
 1.ajaxForm, 这个方法在调用时不是马上提交,只是说明调用的Form要以ajax方式提交,该方法一般在$(document).ready方法里设置。

2.
ajaxSubmit,这个方法在调用时就会马上提交。
var options = { 
    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
 
  
   <% using (Ajax.BeginForm( " action " , " controll " , new AjaxOptions
{
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结合使用
前端代码


 
  
< script type ="text/javascript" language ="javascript" src ="http://www.cnblogs.com/Scripts/jquery-1.4.1.min.js" ></ script >
< script type ="text/javascript" language ="javascript" src ="http://www.cnblogs.com/Scripts/jquery.validate.min.js" ></ script >
< script type ="text/javascript" language ="javascript" src ="http://www.cnblogs.com/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 " );
}


转载于:https://www.cnblogs.com/wangjq/archive/2011/03/08/1977301.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值