2017-04-14 2 views
2

Ich habe eine ajax-Funktion geschrieben, in der ich Bestätigungsmeldungen anzeigen möchte, bevor ich das Formular abschicke. Wie soll ich meinen Zustand ergänzen? Unten ist mein Code.Bestätigungsmeldung anzeigen, bevor eine AJAX-Anforderung gesendet wird

$.ajax({ 
         url: "UBRDashboard.aspx/GetDllValue", 
         dataType: "json", 
         type: "POST", 
         contentType: 'application/json; charset=utf-8', 
         data: JSON.stringify({ ddlOduModel: ddlOduModel, ddlAntModel: ddlAntModel, ddlOMTModel: ddlOMTModel, ddlSapID: ddlSapID, ddlVendorName: ddlVendorName, strReqID: r.d, ddlSapDescVal: ddlSapDescVal, SITE_ADD: SITE_ADD, LATITUDE: LATITUDE, LONGITUDE: LONGITUDE, ddlEQP_SEQ: ddlEQP_SEQ, txtLinkID: txtLinkID, RJ_QUANTITY: RJ_QUANTITY, USER_NAME: USER_NAME, CREATED_DATE: CREATED_DATE, LOCATIONTYPE: LOCATIONTYPE, TOWERTYPE: TOWERTYPE }), 
         async: true, 
         processData: false, 
         cache: false, 
         success: function (r) { 
          if (r.d == "OK") { 
           alert('Record Saved successfully'); 
           window.location.href = "UBRDashboard.aspx"; 
          } 
         }, 
         error: function (xhr) { 
          alert('Error while selecting list..!!'); 
          window.location.href = "ErrorPage.aspx"; 
         } 
        }) 
       }, 
       error: function (xhr) { 
        alert('Error while selecting list..!!'); 
        window.location.href = "ErrorPage.aspx"; 
       } 

Antwort

3

Die Lösung ist beforeSend Ajax-Eigenschaft zu verwenden.

beforeSend ist ein Pre-Anfrage Callback-Funktion, bevor es sent.Returning falsch in der beforeSend Funktion ist die Anfrage abzubrechen.

beforeSend:function(){ 
    return confirm("Are you sure?"); 
}, 

AJAX

$.ajax({ 
     url: "UBRDashboard.aspx/GetDllValue", 
     dataType: "json", 
     type: "POST", 
     contentType: 'application/json; charset=utf-8', 
     data: JSON.stringify({ ddlOduModel: ddlOduModel, ddlAntModel: ddlAntModel, ddlOMTModel: ddlOMTModel, ddlSapID: ddlSapID, ddlVendorName: ddlVendorName, strReqID: r.d, ddlSapDescVal: ddlSapDescVal, SITE_ADD: SITE_ADD, LATITUDE: LATITUDE, LONGITUDE: LONGITUDE, ddlEQP_SEQ: ddlEQP_SEQ, txtLinkID: txtLinkID, RJ_QUANTITY: RJ_QUANTITY, USER_NAME: USER_NAME, CREATED_DATE: CREATED_DATE, LOCATIONTYPE: LOCATIONTYPE, TOWERTYPE: TOWERTYPE }), 
     async: true, 
     processData: false, 
     cache: false, 
     beforeSend:function(){ 
     return confirm("Are you sure?"); 
     }, 
     success: function (r) { 
     if (r.d == "OK") { 
     alert('Record Saved successfully'); 
     window.location.href = "UBRDashboard.aspx"; 
     }, 
     error: function (xhr) { 
      alert('Error while selecting list..!!'); 
      window.location.href = "ErrorPage.aspx"; 
     } 
}); 
+1

Das ist perfekt durch die Ajax .. behandelt !! Arbeiten wie ein Charme – BNN

0

Schreiben Sie Ihre Ajax in eine Funktion wie:

function save(){ 
    // something in here 
} 

Danach eine Bestätigung Funktionalität schreiben, wenn der Benutzer bestätigen dann speichern call() Funktion

0

Vielleicht exemple ist, was Sie brauchen?

var r = confirm("Press a button!"); 
if (r == true) { 
    // Make your ajax call here 
} else { 
    // He refused the confirmation 
} 

Rufen Sie Ihre Bestätigung vor dem Ajax-Anruf?

0
if (confirm("Do you want to Submit?")) { 
    // If you pressed OK!"; 

$.ajax({ 
    url: "UBRDashboard.aspx/GetDllValue", 
    dataType: "json", 
    type: "POST", 
    contentType: 'application/json; charset=utf-8', 
    data: JSON.stringify({ ddlOduModel: ddlOduModel, ddlAntModel: ddlAntModel, ddlOMTModel: ddlOMTModel, ddlSapID: ddlSapID, ddlVendorName: ddlVendorName, strReqID: r.d, ddlSapDescVal: ddlSapDescVal, SITE_ADD: SITE_ADD, LATITUDE: LATITUDE, LONGITUDE: LONGITUDE, ddlEQP_SEQ: ddlEQP_SEQ, txtLinkID: txtLinkID, RJ_QUANTITY: RJ_QUANTITY, USER_NAME: USER_NAME, CREATED_DATE: CREATED_DATE, LOCATIONTYPE: LOCATIONTYPE, TOWERTYPE: TOWERTYPE }), 
    async: true, 
    processData: false, 
    cache: false, 
    beforeSend:function(){ 
    return confirm("Are you sure?"); 
    }, 
    success: function (r) { 
    if (r.d == "OK") { 
    alert('Record Saved successfully'); 
    window.location.href = "UBRDashboard.aspx"; 
    }, 
    error: function (xhr) { 
     alert('Error while selecting list..!!'); 
     window.location.href = "ErrorPage.aspx"; 
    } 
}); 

} else { 
    // If you pressed Cancel!"; 

} 

Bitte überprüfen Sie mit window.confirm

+0

so vor Ajax-Aufruf, ich habe den confirm Teil schreiben ?? – BNN

+0

Ja, Sie müssen den Ajax-Anruf mit diesem wenn sonst wickeln. – Tuhin

0

Verwenden ajax beforeSend Callback-Funktion.

beforeSend: function() { 
        if(confirm("Are you sure?")){ 
         // do something 
        } else { 
         // stop the ajax call 
         return false; 
        } 
       }, 

Siehe Dokumentation Ajax http://api.jquery.com/jquery.ajax/

Verwandte Themen