【问题标题】:show alert php to axios向 axios 显示警报 php
【发布时间】:2022-11-19 19:14:01
【问题描述】:

我想在 php 中向 axios 发出警告,但它不起作用。

axios.post(url, formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
.then(function(res) {
  alert("Successfully enrolled.\nPress ok to go back");

})
.catch(function(error) {
  alert("error");
})

PHP代码

if($bal>=2500) {

} else {
  echo '<script language="javascript">';
  echo 'alert("insufficient balance")';
  echo '</script>';
}

如果$bal 小于 2500,我希望它发出警报,但警报不会出现。

【问题讨论】:

  • 现在这有两个误解。 1)你根本没有处理响应的值(当承诺解决时)和 2)如果你愿意,那么你最好在你的 Javascript 中“创建”警报(例如,在处理响应时)。

标签: javascript php axios


【解决方案1】:

这是使这项工作可行的方法。 您的 Javascript 可能如下所示:

axios.post(url, formData, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
.then(function(res) {
  if(res.data.success === true){
    alert("Successfully enrolled.
Press ok to go back");  
  }else{
    alert(res.data.message);
  }
)
.catch(function(error) {
  alert("error");
})

PHP

$response = array();
if($bal>=2500) {
  $response['success'] = true;
  $response['message'] = 'All ok';
} else {
  $response['success'] = false;
  $response['message'] = 'insufficient balance';
}
//this assumes that this is the only thing returned from PHP when making the ajax call.
echo json_encode($response);

【讨论】:

    【解决方案2】:

    使用 JavaScript ,这意味着在客户端。

    【讨论】:

      猜你喜欢
      • 2019-08-21
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 2021-10-19
      • 2021-08-11
      • 1970-01-01
      相关资源
      最近更新 更多