Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display confirmation message before send ajax request

I have written an ajax function where I want to display confirmation meeessage before submitting the form. How should I add with my condition. Below is my code.

$.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";
                }
like image 527
Nad Avatar asked Apr 14 '17 10:04

Nad


Video Answer


2 Answers

The solution is to use beforeSend ajax property.

beforeSend is a pre-request callback function before it is sent.Returning false in the beforeSend function will cancel the request.

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";
      }
});
like image 76
Mihai Alexandru-Ionut Avatar answered Oct 18 '22 05:10

Mihai Alexandru-Ionut


Use ajax beforeSend callback function.

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

See documentation Ajax http://api.jquery.com/jquery.ajax/

like image 37
A.Chiesa Avatar answered Oct 18 '22 06:10

A.Chiesa