2017-04-12 16 views
0

In meiner asp.net Mvc app machen, habe ich Schaltfläche klicken, die zu einem Javascript Punkte, dieWie Javascript ausführen Funktion synchron

function OnButtonClick(s, e, startUrl, progressUrl) { 
    Fetch(progressUrl); 
    ImportUpdate(startUrl); 
    } 

Aktion ajax jquery an einen Controller Fetch und ImportUpdate sind aufruft.

function Fetch(progressUrl) { 
     positionDate = ReportingPositionDate.GetDate().toDateString(); 
     $.ajax({ 
      type: 'POST', 
      url: "@Url.Action("BloombergFet", "ImportData")", 
      data: JSON.stringify({ positionDate: positionDate }), 
      dataType: "text", 
      contentType: "application/json; charset=utf-8", 
      beforeSend: function() { lpBloomberg.Show(); }, 
      success: function (msg) { 
       ImportSuccessMessage.SetText(msg); 
       lpBloomberg.Hide(); 
       lpImport.Show(); 

      }, 
      error: function (xhr, textStatus, errorThrown) { 
       lpBloomberg.Hide() 

      } 
     }); 
    } 

function ImportUpdate(progressUrl) { 
     positionDate = ReportingPositionDate.GetDate().toDateString(); 
     myProgressBar.Show; 
     $.ajax({ 
      type: 'POST', 
      url: "@Url.Action("ProcessImportRecord", "ImportData")", 
      data: JSON.stringify({ positionDate: positionDate }), 
      dataType: "text", 
      contentType: "application/json; charset=utf-8", 
      beforeSend: function() { lpImport.Show(); }, 
      success: function (msg) { 
      ImportDataGridView.PerformCallback(); 
      ImportSuccessMessage.SetVisible(true); 
      ImportSuccessMessage.SetText(msg); 
      lpImport.Hide(); 
     }, 
     error: function (xhr, textStatus, errorThrown) { 
      ImportErrorMessage.SetVisible(true); 
      ImportErrorMessage.SetText(xhr.statusText) 
     } 
    }); 
    } 

Derzeit beide Methoden Fetch(progressUrl) und ImportUpdate(progressUrl) sind zugleich genannt. Ich möchte Fetch(progressUrl) abschließen und anschließend ImportUpdate ausführen.

Wie erreiche ich das? Schätze alle Hilfe.

+1

Rückrufe. Rückrufe verwenden –

+1

Wenn Sie immer Fetch gefolgt von ImportUpdate abrufen möchten, würde ich vorschlagen, dass Sie sie zu einem einzigen API-Aufruf kombinieren. – James

+0

danke James. Das hat funktioniert – ProgSky

Antwort

2

Rufen Sie Ihre zweite Funktion ImportUpdate (progressUrl) in den Erfolg Block der ersten Funktion Fetch (progressUrl) wie folgt:

function Fetch(progressUrl) { 
    positionDate = ReportingPositionDate.GetDate().toDateString(); 
    $.ajax({ 
     type: 'POST', 
     url: "@Url.Action("BloombergFet", "ImportData")", 
     data: JSON.stringify({ positionDate: positionDate }), 
     dataType: "text", 
     contentType: "application/json; charset=utf-8", 
     beforeSend: function() { lpBloomberg.Show(); }, 
     success: function (msg) { 
      ImportSuccessMessage.SetText(msg); 
      lpBloomberg.Hide(); 
      lpImport.Show(); 

      //Place call for ImportUpdate function here, like so 
      ImportUpdate(startUrl); 
     }, 
     error: function (xhr, textStatus, errorThrown) { 
      lpBloomberg.Hide() 

     } 
    }); 
} 

Doch wie James wies darauf hin, wenn Sie ImportUpdate nach jeder Zeit anrufen möchten Wenn Fetch aufgerufen wird, ist es sinnvoller, sie einfach zu kombinieren, außer Sie rufen ImportUpdate unabhängig woanders auf, wenn Fetch nicht zuerst aufgerufen wird.

BTW, die Rückrufe Kevin B. wahrscheinlich bezieht sich mit dem jQuery .post() Funktion verwendet, die Sie wie folgt verwenden können:

// Assign handlers immediately after making the request, 
// and remember the jqxhr object for this request 
var jqxhr = $.post("example.php", function() { 
    alert("success"); 
}) 
    .done(function() { 
    alert("second success"); 
    }) 
    .fail(function() { 
    alert("error"); 
    }) 
    .always(function() { 
    alert("finished"); 
}); 

// Perform other work here ... 

// Set another completion function for the request above 
jqxhr.always(function() { 
    alert("second finished"); 
}); 

so anstatt, Ihre Funktionsaufruf in den Erfolg Fetch-Funktion Rückruf Ihrer aktuellen Sie würde es wie so in .done Rückruf setzen:

.done(function() { 
    ImportUpdate(startUrl); 
    }) 
    .fail(function() { 
    //handle errors 
    }) 
0

Put ImportUpdate(progressUrl) in Ihrem Erfolg Callback-Funktion für Fetch(progressUrl)

Verwandte Themen