如何 – 在成功提交表单后以jquery ajax格式提醒?

我试图通过jquery $.ajax();提交PHP表单jquery $.ajax(); 。 它成功提交,但是当我试图提醒消息时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); 其他技巧。 我的问题是为什么警报不起作用,整个脚本工作时以及为什么它会给出解析错误?

SUCCESS不是变量而是字符串。 你需要在alert("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) { ... } 

如果你想坚持你的代码。

  1. 确保data是有效的JSON
  2. data.error来自服务器,它可能不是你想象的那样。 使用console.log确定其真实值。 更进一步,将其改为更有意义的东西

data.status =>’成功’或’失败’

简单。 删除 – dataType:’json’; 使用 – formdata发送所有数据。

    Ajax Image Upload Using PHP and jQuery        

Facebook Update







loading..