2012-11-12 45 views
-1

我有一個控制器兩種方法:PageLoad和。 Page回報viewLoad回報JsonResult。我也有一個與我打電話給我的方法jquery-functions的看法。調用控制器方法與jquery異步

他們在執行訂單,但我希望異步調用它們。我怎麼做?

function **LoadPage**(page, isnext) { 
    $(':focus').blur(); 
    $('#loadingBlockElement').show(); 

    $.ajax({ 
     type: "GET", 
     url: '@Html.Raw(Url.Action("page", new { formId = Model.Params.FormId, userId = Model.Params.UserId, baseVersion = Model.Params.BaseVersion, stateName = Model.Params.StateName, stateVersion = Model.Params.StateVersion, stateFormId = Model.Params.StateFormId, baseFormId = Model.Params.BaseFormId }))'+'&page='+page+'&isnext='+isnext,   

     success: function (result) {  
      $('body').find('.ui-dialog').find('div').remove();   
      $('body').find('.ui-dialog').remove();  
      WE = null; 
      $('#main').html(result); 
      $('form').hide(); 
     } 
    }); 
} 

function **Load**() {   
    $.ajax({ 
     type: "POST", 
     url: "@Url.Action("load")"+'[email protected]'+'&[email protected]'+'&[email protected]'+'&[email protected]'+'&[email protected]'+'&[email protected]'+'&[email protected]'+'&page='+$('form:first').attr('ID'), 
     success: function (result) {..bla-bla-bla 

控制器

負載:

public JsonResult Load(int formId, int baseVersion, string stateName, int stateVersion, string page, string userId, int stateFormId, int baseFormId) 
{ 
    ... 
} 

頁:

public ActionResult Page(int formId, int baseVersion, string stateName, int stateVersion, string page, string userId, bool? isNext, int stateFormId, int baseFormId) 
{ 
    ... 
} 
+2

究竟什麼是你的問題? –

+0

如果我正確理解你的問題,這是你需要什麼http://api.jquery.com/jQuery.when/ –

+0

我需要調用我的方法異步。他們應該並行執行。 –

回答

1

設置同時在$.ajax電話as the documentation shows的0屬性爲真正,然後調用每個函數隨後:

$(document).ready(function() { 
    LoadPage(page); 
    Load(); 
}); 

function LoadPage(page) { 
    $.ajax({ 
     type: "GET", 
     async: true, // this is what you're missing 
     url: "yoururl", 
     success: function (result) { 
      // handle success 
     } 
    }); 
} 

function Load() {   
    $.ajax({ 
     type: "POST", 
     async: true, // this is what you're missing 
     url: "yoururl", 
     success: function (result) { 
      // handle success 
     } 
    }); 
} 
+0

因此,我不需要在控制器中使用任何屬性進行異步操作? –

+0

@NickitaFabregas不,控制器操作是一種服務器端功能,不管你是否異步調用它。唯一需要關注的是客戶端 - 在這種情況下,您的jQuery ajax調用。 –

+0

謝謝。它幫助了我。 –