SweetAlert:缺少标题参数

时间:2017-06-03 22:15:24

标签: javascript sweetalert

我正在使用甜蜜警报,当从甜蜜警报调用错误功能时,我遇到以下错误。当php文件的操作成功时,它工作正常。怎么能解决这个问题?非常感谢

SweetAlert: Missing "title" argument!

这是我的javascript代码

function DeletePost() {
    swal({
        title: "Sei Sicuro?",
        text: "Questa operazione non è reversibile ed eliminerà la comunicazione i commenti ad essa connessi dal database!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Si, elimina comunicazione!",
        cancelButtonClass: "btn btn-danger",
        cancelButtonText: "No, non procedere!",
        closeOnConfirm: false,
        closeOnCancel: false
    }, function(isConfirm) {
        if (isConfirm) {

            $.ajax({
                url: "../delete_all.php",
                method: "POST",
                dataType: 'json',

                success: function(response) {
                  // swal('Deleted!', response.message, response.status);
                    swal({

                      title: response.title, 
                      text: response.message, 
                      type: response.status

                    },

                    function(){ 

                      location.reload();

                    }

                  );

                },

                error: function(response) {

                  swal({

                      title: response.title,
                      text: response.message, 
                      type: response.status

                    });


                }



            }); 

        } else {
            swal("Annulato!", "Operazione annullata con successo!", "error");
        }
    });
}

这是我的php文件

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Prelevo l'id dell'amministratore e lo passo ad una variabile
    $userid = $_SESSION['user_id'];

    if($delete_inbox  = mysqli_prepare($conn, "DELETE FROM user_inbox where user_inbox_user=? AND user_inbox_status = 'trash'")){ 

           mysqli_stmt_bind_param($delete_inbox, 'i', $userid);
           mysqli_stmt_execute($delete_inbox);
           mysqli_stmt_close($delete_inbox);

    // Passo messaggio di risposta se l'operazione è andata a buon fine
    $response['title']  = 'Messaggi eliminati!';
    $response['message'] = 'Tutti i messaggi sono stati eliminati con successo.';
    $response['status']  = 'success';

    }else{

    // Passo messaggio di risposta se l'operazione non è andata a buon fine
    $response['title']  = 'Si è verificato un errore!';
    $response['message'] = 'Non è stato possibile eliminare i messaggi. Per favore contatta amministratore di sistema';
    $response['status']  = 'error';

    }

    echo json_encode($response);

}

3 个答案:

答案 0 :(得分:1)

您忘记将json的回复转换为javascript对象

所以应该没事。

 $.ajax({
      url: "../delete_all.php",
      method: "POST",
      dataType: 'json',
      success: function(response) {
         // swal('Deleted!', response.message, response.status);

         response= JSON.parse(response);

         swal({
            title: response.title, 
                  ...

我添加了这一行

  response= JSON.parse(response);

答案 1 :(得分:0)

swal({
  title: response.title || 'TITLE IS NOT SET', 
  text: response.message || 'MESSAGE IS NOT SET', 
  type: response.status || 'STATUS IS NOT SET'
}, function() {

});

怎么样

swal({
  title: JSON.parse(response.respoonseText).title
  // ...
}, function() {

});
response = JSON.parse(response.responseText);
swal({
  title: response.title || 'TITLE IS NOT SET', 
  text: response.message || 'MESSAGE IS NOT SET', 
  type: response.status || 'STATUS IS NOT SET'
}, function() {

});

答案 2 :(得分:0)

非常感谢你的帮助,它认为问题是在php文件中我已经改变了它现在它工作正常,我之所以要测试它是因为我可以在我之前验证和消毒变量对数据库进行查询,如果无法执行查询,我想显示错误

这是我的php文件

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $user_id = 'prova';

    if (!filter_var($user_id, FILTER_VALIDATE_INT)) {
        // Passo messaggio di risposta se l'operazione non è andata a buon fine
    $response['title']  = 'Si è verificato un errore!';
    $response['message'] = 'Non è stato possibile eliminare i messaggi. Per favore contatta amministratore di sistema';
    $response['status']  = 'error';

    echo json_encode($response);

    }else{

    // Prelevo l'id dell'amministratore e lo passo ad una variabile effettuaando un sanitize
    $user_id = filter_var($_SESSION['user_id'], FILTER_SANITIZE_NUMBER_INT);

    $delete_inbox  = mysqli_prepare($conn, "DELETE FROM user_inbox where user_inbox_user=? AND user_inbox_status = 'trash'");

           mysqli_stmt_bind_param($delete_inbox, 'i', $userid);
           mysqli_stmt_execute($delete_inbox);
           mysqli_stmt_close($delete_inbox);

    // Passo messaggio di risposta se l'operazione è andata a buon fine
    $response['title']  = 'Messaggi eliminati!';
    $response['message'] = 'Tutti i messaggi sono stati eliminati con successo.';
    $response['status']  = 'success';

    echo json_encode($response);

    }

要生成错误,我将$ user_id设置为非整数

现在没有使用它正常工作 title:JSON.parse(response.respoonseText).title 要么 response = JSON.parse(response);