javascript - 如何 - 在成功提交表单后以 jquery ajax 形式发出警报?

标签 javascript php jquery ajax

我正在尝试通过 jquery $.ajax(); 提交一个 PHP 表单。它提交成功,但是当我尝试提醒消息时 - alert(SUCCESS); 成功。它不是。有什么猜测吗?

代码:

$.ajax({
  url: 'basic_cms_manager_home_fb_form_submit.php',
  type: 'POST',
  data: formData,
  cache: false,
  dataType: 'json',
  success: function(data, textStatus, jqXHR) {
    if (typeof data.error === 'undefined') {
      // Success so call function to process the form
      alert("SUCCESS");
      console.log('SUCCESS: ' + data.success);
    } else {
      // Handle errors here
      console.log('ERRORS: ' + data.error);
    }
  },
  error: function(jqXHR, textStatus, errorThrown) {
    // Handle errors here
    console.log('ERRORS: ' + textStatus);
  },
  complete: function() {
    // STOP LOADING SPINNER
  }
});

注意:我已经尝试过:console.log(data); n 其他技巧。我的问题是为什么 Alert 不工作,当整个脚本工作时以及为什么它给出 parseerror?

最佳答案

SUCCESS 不是一个变量而是一个字符串。你需要在它周围加上引号,比如 alert("SUCCESS");

.success.error 方法的使用也已被弃用。使用 .done.fail 或者您可以简单地执行以下操作

 $.ajax({
    url: 'basic_cms_manager_home_fb_form_submit.php',
    type: 'POST',
    data: formData,
    cache: false,
    dataType: 'json',
    success: function(data)
    {

    alert("SUCCESS");       
    console.log('SUCCESS: ' + data.success);

    },
    error: function(jqXHR, textStatus, errorThrown)
    {
    // Handle errors here
    console.log('ERRORS: ' + textStatus);
    },
    complete: function()
    {
    // STOP LOADING SPINNER
    }
    });

另一件事

Typeof null 返回一个对象,因此如果 data.errors 为 null,您的检查将失败。考虑做

if (!data.errors) {
    ...
}

如果你想坚持你的代码。

关于javascript - 如何 - 在成功提交表单后以 jquery ajax 形式发出警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704743/

相关文章:

javascript - 从函数返回的 View 不会在状态更改时更新

php - 我可以将所有 SQL 查询写入 phpMyAdmin/HTML 文件吗?还是需要使用其他程序?

javascript - 需要像工具提示一样的 html 弹出窗口帮助

jquery css 动态或静态 html 元素

javascript - 向大型 DOM 添加类时的性能问题

Javascript 和 lodash ... 将 _.find 结果转换为 bool 值

javascript - 如何比较两个百分比字符串

javascript - 使用 jQuery 根据另一列的文本求和列总计

javascript - 使用 Javascript、PHP、PDO、CSS、html 显示谁在线/离线

远程服务器 ssh2 中的 php chown 文件