成功提交表单后,如何以jquery ajax表单发出警报? [英] How to - alert ,in jquery ajax form, after successful form submission?

查看:60
本文介绍了成功提交表单后,如何以jquery ajax表单发出警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jquery $.ajax();提交一个PHP表单.它已成功提交,但是当我尝试向消息alert(SUCCESS);发出警报时成功.不是.有任何猜测吗?

I am trying to submit a PHP form, via jquery $.ajax(); . Its submitting successfully, but when I am trying to alert a message- alert(SUCCESS); on success. It's not. Any guesses ?

代码:

$.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);其他技巧.我的问题是,为什么警报无法正常工作,整个脚本何时正常工作,为什么会发出parseerror?

NOTE: I already tried : console.log(data); n other tricks. My question is why Alert is not working, when entire script is working and why it's giving parseerror?

推荐答案

SUCCESS不是变量,而是字符串.您需要在其周围加上引号,例如alert("SUCCESS");

SUCCESS is not a variable but a string. You need to put quotes around it like alert("SUCCESS");

也不建议使用.success.error方法.使用.done.fail代替,或者您可以简单地执行以下操作

Also the use of .success and .error methods have been deprecated. Use .done and .fail instead or you can simply do the following

 $.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为空,则检查将失败.考虑做

Typeof null returns an object, so if data.errors is null, your check will fail. Consider doing

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

如果您想保留自己的代码.

if you want to persist with your code.

这篇关于成功提交表单后,如何以jquery ajax表单发出警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆