简体   繁体   English

在发送ajax请求之前显示确认消息

[英]Display confirmation message before send ajax request

I have written an ajax function where I want to display confirmation meeessage before submitting the form.我已经编写了一个ajax函数,我想在提交表单之前显示确认信息。 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";
                }

The solution is to use beforeSend ajax property.解决方案是使用beforeSend ajax 属性。

beforeSend is a pre-request callback function before it is sent.Returning false in the beforeSend function will cancel the request. beforeSend 是发送之前的预请求回调函数。beforeSend 函数中返回false 将取消请求。

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

AJAX 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";
      }
});

Use ajax beforeSend callback function.使用 ajax beforeSend 回调函数。

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/请参阅文档 Ajax http://api.jquery.com/jquery.ajax/

Write your ajax into a function like:将您的 ajax 写入如下函数:

function save(){
   // something in here 
}

After that write a confirmation functionality, if user confirm then call save() function之后写一个确认功能,如果用户确认然后调用 save() 函数

Maybe this exemple is what you need ?也许这个例子是你需要的?

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

Call your confirm before ajax call ?在 ajax 调用之前打电话确认?

您可以尝试将确认消息放在beforeSend方法中: http : //api.jquery.com/jquery.ajax/

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!";

}

Please check with window.confirm请与window.confirm核对

I ran into this issue recently, so this is my answer, I am using jquery and jqueryconfirm, the "beforesend" callbak only allows the standard "alert" and "confirm" functions.我最近遇到了这个问题,所以这是我的答案,我使用 jquery 和 jqueryconfirm,“beforesend”callbak 只允许标准的“alert”和“confirm”功能。

What I did is placing a "fake" submit button and hide the actual submit one, so it was easy dealing with the response from the custom confirm dialogs, once I got the affirmative answer from the dialog I call the "click" method of the hidden submit button.我所做的是放置一个“假”提交按钮并隐藏实际提交按钮,因此很容易处理来自自定义确认对话框的响应,一旦我从对话框中得到肯定的答案,我就会调用隐藏的提交按钮。

...
<button id="confirmSave" type="button">Save</button>
<button id="save" class="is-hidden" type="submit"></button>
<button id="close" aria-label="close" type="reset">Cancel</button>
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM