2017-06-03 249 views
0

我使用甜蜜的警報,並且從甜蜜警報調用錯誤函數時出現以下錯誤。它從PHP文件的操作成功時工作正常。如何解決這個問題?非常感謝SweetAlert:缺少標題參數

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

} 
+0

你有工作嗎? –

回答

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() { 

}); 
+0

嗨,感謝您的回答,他們被設置到php文件中,我使用json調用它們,當操作成功時它工作正常,但如果出現錯誤則不會。對不起,我不明白你的答案:( – pippo

+0

你有php錯誤啓用? –

+0

嗨是的PHP錯誤啓用 – pippo

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); 
+0

Ciao,Gianfrancesco我試過你的解決方案,現在鉻控制檯給出這個錯誤:Uncaught SyntaxError:意外的令牌o JSON在位置1 – pippo

+0

嘗試'console.log(response)'和'console.log(response.responseText)' –

+0

不應該是'response = JSON.parse(response.responseText);'? –

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的錯誤不整

它現在不使用工作正常 標題:JSON.parse(response.respoonseText).title 或 response = JSON.parse(response);