简体   繁体   English

如何使用jQuery AJAX和JSON确认使用Bootbox提交表单

[英]How to confirm a form submission with Bootbox using jQuery AJAX and JSON

I'm working in a web application using Spring MVC . 我正在使用Spring MVC在Web应用程序中工作。 I'm trying to show a confirmation dialog before submitting a form using Bootbox , but I'm getting a 500 internal server error. 我在使用Bootbox提交表单之前尝试显示确认对话框,但是我收到500内部服务器错误。

This is my form: 这是我的表格:

<form id="checkout-form" class="smart-form" novalidate="novalidate">
  ...some fields
  <button type="button" class="btn btn-primary" onclick="insertFunction()">
    Accept
  </button>
</form>

This is my insertFunction() 这是我的insertFunction()

function insertFunction(){

  var name = $('#name').val();
  var lastname = $('#lastname').val();

  var confirmSend;

  var json = {"name": name,"lastname": lastname};

  $.ajax({
    type: "POST",
    url: "register/insertPerson",
    data: JSON.stringify(json),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    cache: false,
    beforeSend: function (xhr){
      bootbox.dialog({
        message: "I am a custom dialog",
        title: "Custom title",
        buttons: {
          success: {
            label: "Success!",
            className: "btn-success",
            callback: function() {
              //I DONT KNOW WHAT TO DO HERE
            }
          },
          danger: {
            label: "Danger!",
            className: "btn-danger",
            callback: function() {
              return false;
            }
          }
        }
      });

      xhr.setRequestHeader("Accept", "application/json");
      xhr.setRequestHeader("Content-Type", "application/json");
    },
    success: function (data){
      if (data.message === true){
        bootbox.alert("OPERATION WAS EXECUTED WITHOUT PROBLEMS");
      } else {
        bootbox.alert("OPERATION FAILED");
      }
    },
    error: function (xhr, ajaxOptions, thrownError){
      alert(xhr.status);
      alert(xhr.responseText);
      alert(thrownError);
    }
  });
}

I send the name and last name value to my spring controller and I do a insert and if it was successfully my controller method returns true, and in the success block of my script I check the value if is true I show a message and if it's false i show a different message. 我将名称和姓氏值发送到我的spring控制器并进行插入,如果成功,我的控制器方法返回true,并在我的脚本的成功块中检查值是否为true我显示一条消息,如果是我显示不同的消息。

My problem is that i don't know what to do inside this section: 我的问题是我不知道该部分内该做什么:

 success: {
  label: "Success!",
    className: "btn-success",
      callback: function() {
        //I DONT KNOW WHAT TO DO HERE
      }
}

*EDIT: * It was my fault submiting a value in the form fields, now I'm not getting a 500 internal server error, now it show me my FAILED dialog that i fire in the success block *编辑:*我在表单字段中提交值是我的错,现在我没有得到500内部服务器错误,现在它显示我在成功块中触发的FAILED对话框

else {
        bootbox.alert("OPERATION FAILED");

and then after this message is sho, down under it, it shows the confirmation diaglog that i want to show firts. 然后在这个消息之后,在它下面,它显示我要显示的确认diaglog。 Is like is showing me the messages in the wrong order. 就像是以错误的顺序向我显示消息。 Also when i hit the Danger button it don't close the dialog window only if i press Success. 此外,当我按下危险按钮时,仅当我按下成功时才会关闭对话框窗口。

*EDIT 2: * *编辑2:*

The confirmation windows is working, but I'm having problems when i press the cancel button, when i press the cancel button the window don't close. 确认窗口正在工作,但是当我按下取消按钮时遇到问题,当我按下取消按钮时窗口没有关闭。

 function preInsert()
{
    bootbox.dialog({
        message: "are you sure",
        title: "are you sure",
        buttons: {
            success: {
                label: "Acept",
                className: "btn-success",
                callback: function () {
                    realInsert();
                }
            },
            danger: {
                label: "Cancel",
                className: "btn-danger",
                callback: function () {
                    return false;
                }
            }
        }
    });
}

function realInsert() {

    var name= $('#name').val();
    var lastName= $('#lastName').val();


    var json = {"name": name,
        "lastName": lastName
    };


    $.ajax(
            {
                type: "POST",
                url: "register/insertForm",
                data: JSON.stringify(json),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                beforeSend: function (xhr)
                {
                    xhr.setRequestHeader("Accept", "application/json");
                    xhr.setRequestHeader("Content-Type", "application/json");
                },
                success: function (data)
                {



                    if (data === true)
                    {
                        bootbox.alert("Insert Successfully ");
                    }
                    else
                    {
                        bootbox.alert("Problem during the insert");
                    }
                },
                error: function (xhr, ajaxOptions, thrownError)
                {
                    alert(xhr.status);
                    alert(xhr.responseText);
                    alert(thrownError);
                }
            });
}

Try making the ajax request only after the user clicks the success button.The callback function will be called after the button is clicked. 只有在用户单击成功按钮后才尝试发出ajax请求。单击按钮后将调用回调函数。

 function insertFunction() { bootbox.dialog({ message: "I am a custom dialog", title: "Custom title", buttons: { success: { label: "Success!", className: "btn-success", callback: function() { success(); } }, danger: { label: "Danger!", className: "btn-danger", callback: function() { return false; } } } }); } function success() { var name = $('#name').val(); var lastname = $('#lastname').val(); var confirmSend; var json = { "name": name, "lastname": lastname }; $.ajax({ type: "POST", url: "register/insertPerson", data: JSON.stringify(json), contentType: "application/json; charset=utf-8", dataType: "json", cache: false, success: function(data) { if (data.message === true) { bootbox.alert("OPERATION WAS EXECUTED WITHOUT PROBLEMS"); } else { bootbox.alert("OPERATION FAILED"); } }, error: function(xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(xhr.responseText); alert(thrownError); } }); 

My answer is for your "EDIT 2" version: If you remove the "return false;" 我的回答是你的“EDIT 2”版本:如果你删除“return false;” command from your "Danger!" 从你的“危险!”命令 button's callback, it will work properly. 按钮的回调,它将正常工作。 Also the Accept button will work properly. “接受”按钮也可正常工作。

If you ever come back to read this comment, please accept Pabreetzio's comment, that he made on Apr 17 at 21:06, since he solved your problem there. 如果您回过头来阅读此评论,请接受Pabreetzio在4月17日21:06发表的评论,因为他在那里解决了您的问题。

If you're getting a 500 internal server error then it sounds like you're submitting the form and hitting the server. 如果您收到500内部服务器错误,那么听起来您正在提交表单并点击服务器。 There may be a problem with the server side code, which you don't discuss here that is giving you that error. 服务器端代码可能存在问题,您在此处未讨论该代码会给您带来错误。

In order for your confirmation dialogue to work the beforeSend function must return false when the Danger! 为了让您的确认对话工作, beforeSend函数必须在Danger!时返回false Danger! button is pressed. 按下按钮。 Try returning false from the second callback function: 尝试从第二个回调函数返回false:

danger: {
  label: "Danger!",
  className: "btn-danger",
  callback: function() {
    return false;
  }
}

 function insertFunction() { bootbox.dialog({ message: "I am a custom dialog", title: "Custom title", buttons: { success: { label: "Success!", className: "btn-success", callback: function() { success(); } }, danger: { label: "Danger!", className: "btn-danger", callback: function() { return false; } } } }); } function success() { var name = $('#name').val(); var lastname = $('#lastname').val(); var confirmSend; var json = { "name": name, "lastname": lastname }; $.ajax({ type: "POST", url: "register/insertPerson", data: JSON.stringify(json), contentType: "application/json; charset=utf-8", dataType: "json", cache: false, success: function(data) { if (data.message === true) { bootbox.alert("OPERATION WAS EXECUTED WITHOUT PROBLEMS"); } else { bootbox.alert("OPERATION FAILED"); } }, error: function(xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(xhr.responseText); alert(thrownError); } }); 

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

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