2017-07-29 32 views
0

私はSweetAlert2ポップアップをAJAXリクエストで使用します。ユーザーがsubmitをクリックすると、私はその要求を実行します。 PHPファイルでは、提出されたデータに対していくつかの検証を行い、結果に応じてSweetAlert2で情報としてユーザーにフィードバックを与えたいと思います。ここでSweetAlert2でのAJAX戻り値の処理

は私SweetAlert2コード:

$('#sweet-ajax').click(function() { 
    swal({ 
     title: "Sure?", 
     text: "Clicking on validated sends the data to our tool.", 
     type: "warning", 
     showCancelButton: true, 
     closeOnConfirm: false, 
     confirmButtonText: "Yes, submit!", 
     cancelButtonText: "Cancel", 
     showLoaderOnConfirm: true, 
     confirmButtonClass: 'btn btn-success', 
     cancelButtonClass: 'btn btn-danger m-l-10', 
     preConfirm: function(givenData){ 
      return new Promise(function(resolve, reject) { 
       setTimeout(function(){ 
        //if statment only for test purposes filled with 2==1 
        if(2 == 1){ 
         swal("Oops", "Sorry something strange happend!", "error") 
        }else{ 
         resolve() 
        } 
       }, 2000) 
      }) 
     }, 
     allowOutsideClick: false 
    }).then(function(givenData){ 
     $.ajax({ 
        type: "post", 
        url: "/assets/php/checkTool.php", 
        data: {registration: "success", amount: ammountInput, email: "[email protected]"}, 
       }) 
     swal({ 
       //only if the response from the AJAX is correct - but how? 
       type: 'success', 
       title: 'Correct!', 
       html: 'All safe! Here is the answer from the tool: ' //need to get the return value of the AJAX request and append it here 
      }) 
    }, function(dismiss) { 

      if (dismiss === 'cancel') { 
      swal(
       'Cancelled', 
       'The action have been cancelled by the user :-)', 
       'error' 
      ) 
      } 
     }) 

}); 

そしてcheckTool.phpファイル:

<?php 
    $registration = $_POST['registration']; 
    $ammountInput= $_POST['ammount']; 
    $email= $_POST['email']; 

    //only some demo things here. Implement it after the SweetAlert2 stuff works properly 
    if ($registration == "success"){ 
     return response(json_encode(array("abc"=>'Success'))); 

    }else{ 
     return response(json_encode(array("abc"=>'Error'))); 

    } 
?> 

どのように私は今SweetAlert2のJavascriptのコードでのAJAX要求からの応答が何であるかを確定することができますか?

SweetAlert2でAJAX応答を処理することは可能ですか?

答えて

0

アヤックスの.done(機能(応答){})関数の内部

}).then(function(givenData){ 
     $.ajax({ 
       type: "post", 
       url: "/assets/php/checkTool.php", 
       data: {registration: "success", amount: ammountInput, email: "[email protected]"}, 
      }).done(function(response) { 
       if(response['abc'] === 'Success') { 
        swal({ 
         type: 'success', 
         title: 'Correct!', 
         html: 'All safe! Here is the answer from the tool: ' + response['answer'] 
        }) 
       } 
      }); 
     }) 
}, function(dismiss) { 
をあなたの甘いアラートをラップ
関連する問題