【问题标题】:Display confirmation message before send ajax request在发送 ajax 请求之前显示确认消息
【发布时间】:2017-09-10 14:25:06
【问题描述】:

我写了一个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,
                        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";
                }

【问题讨论】:

    标签: javascript jquery asp.net ajax


    【解决方案1】:

    解决方法是使用beforeSendajax 属性。

    beforeSend 是一个 pre-request 回调函数 在 beforeSend 函数中返回 false 将取消 请求。

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

    【讨论】:

    • 那;由 ajax 完美处理..!!像魅力一样工作
    【解决方案2】:

    使用 ajax beforeSend 回调函数。

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

    查看文档 Ajax http://api.jquery.com/jquery.ajax/

    【讨论】:

      【解决方案3】:

      将您的 ajax 写入如下函数:

      function save(){
         // something in here 
      }
      

      然后编写一个确认功能,如果用户确认则调用 save() 函数

      【讨论】:

        【解决方案4】:

        也许这个例子就是你需要的?

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

        在 ajax 调用之前调用你的确认?

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:
            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!";
            
            }
            

            请咨询window.confirm

            【讨论】:

            • 所以在ajax调用之前,我必须写确认部分??
            • 是的,如果不这样,你需要用这个包装 ajax 调用。
            【解决方案7】:

            我最近遇到了这个问题,所以这是我的答案,我正在使用 jquery 和 jqueryconfirm,“beforesend”callbak 只允许标准的“alert”和“confirm”函数。

            我所做的是放置一个“假”提交按钮并隐藏实际提交按钮,因此很容易处理来自自定义确认对话框的响应,一旦我从对话框中得到肯定的答案,我称之为“点击”隐藏提交按钮的方法。

            ...
            <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>
            ...
            

            【讨论】:

              猜你喜欢
              • 2017-08-20
              • 2012-10-29
              • 2011-03-26
              • 1970-01-01
              • 1970-01-01
              • 2012-07-04
              • 2015-11-01
              • 2011-02-10
              • 1970-01-01
              相关资源
              最近更新 更多