ejs文件上的res.render sweetalert

时间:2018-09-25 11:05:04

标签: javascript node.js ejs sweetalert

如何通过app.js代码在.ejs文件中显示警报

我正在使用sweetalert npm

<?php

    $list_id = 'hidden';
    $api_key = 'hidden';


    $email = 'abdul12@gmail.com';


    $data_center = substr($api_key,strpos($api_key,'-')+1);

    $url = 'https://'. $data_center .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members';

    $json = json_encode([
        'email_address' => $email,
        'status'        => 'subscribed', 
    ]);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    $result = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    echo $status_code;


    echo "<pre>" ;
     print_r($result) ;

     echo   "</pre>";

?>

1 个答案:

答案 0 :(得分:0)

在app.js中:

if(data.type == 'error')
  {
    res.render('login', {alert: true, alertTitle: "Oops!", alertMessage: "Something went wrong!"})
  }

在login.ejs中

<script>
$(document).ready(function () {

  if (alert === true) {
    var alert = <%= alert %>
    var alertTitle = <%= alertTitle %>
    var alertMessage = <%= alertMessage %>
    swal(alertTitle, alertMessage, "success");
  }

})

</script>