jQuery ajax 成功错误

标签 jquery ajax

我正在尝试提交表单女巫发送电子邮件:

JS

var that = $(this);
$.ajax({
  url: '../wp-content/themes/bsj/php/validate.php',
  type: 'post',
  context: that,
  data: $('#sign-up').serialize(),
  cache: false,
  success: function(){ 
    alert('success!');
  },
  error: function(){
    alert('error!');
  }
});

PHP

/*----------------------------------
    Variables
----------------------------------*/

    // Personal info
    $prefix = $_POST['prefix'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $company = $_POST['company'];
    $email = $_POST['email'];
    $title = $_POST['title'];
    $dept = $_POST['dept'];

    // Contact details
    $address = $_POST['address'];
    $address2 = $_POST['address2'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $phone = $_POST['phone'];
    $ext = $_POST['ext'];
    $fax = $_POST['fax'];

    // Job Summary
    $job_title = $_POST['job_title'];
    $positions = $_POST['positions'];
    $hours = $_POST['hours'];
    $age = $_POST['age'];
    $wage = $_POST['wage'];
    $wage_type = $_POST['wage_type'];
    $job_description = $_POST['job_description'];
    $comments = $_POST['comments'];


/*----------------------------------
    Mail Form
----------------------------------*/

    $to      = 'email@email.com';
    $subject = 'Subject';
    $headers = 'From: noreply@email.com' . "\r\n" .
        'Reply-To: noreply@email.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    $message = 
        '### Personal Information ###' . "\r\n\r\n" .
        'Prefix: ' . $prefix . "\r\n" .
        'First Name: '. $first_name . "\r\n" .
        'Last Name: ' . $last_name . "\r\n" .
        'Company: ' . $company . "\r\n" .
        'E-Mail: ' . $email . "\r\n" .
        'Title: ' . $title . "\r\n" .
        'Dept.: ' . $dept . "\r\n\r\n";

    $message .= 
        '### Contact Details ###' . "\r\n\r\n" .
        'Address: ' . $address . "\r\n" .
        'Address 2: ' . $address2 . "\r\n" .
        'City: ' . $city . "\r\n" .
        'State: ' . $state . "\r\n" .
        'Phone: ' . $phone . "\r\n" .
        'Ext.: ' . $ext . "\r\n" .
        'Fax: ' . $fax . "\r\n\r\n";

    $message .= 
        '### Job Description ###' . "\r\n\r\n" .
        'Job Title: ' . $job_title . "\r\n" .
        'Positions: ' . $positions . "\r\n" .
        'Hours: ' . $hours . "\r\n" .
        'Age: ' . $age . "\r\n" .
        'Wage: ' . $wage . ' - ' . $wage_type .  "\r\n" .
        'Job Description: ' . $job_description . "\r\n" .
        'Comments: ' . $comments;

    mail($to, $subject, $message, $headers);

我设置that是因为我使用模式窗口插件来显示表单,这样我就可以获得正确的范围。

主要问题是,当我单击“提交”时,电子邮件会很好地进入我的收件箱,但它总是执行错误函数而不是成功函数。

这让我发疯,我到处寻找答案,但没有运气。

我收到了几次成功回调,但现在它不再起作用了,我不知道为什么。我很好地收到了电子邮件...

我使用 Chrome 开发者工具检查了响应 header ,得到了 200 OK,因此它正在返回。有什么问题吗?

编辑:好吧,经过 5 个小时的尝试,我今天放弃了。我明天可能会回来尝试其他事情。我现在只使用 complete,效果很好。我认为这可能与服务器有关。客户端正在使用 IIS...

最佳答案

您没有提供 validate.php 代码,所以我很困惑。邮件发送成功后,需要传递JSON格式的数据。 您可以使用json_encode(); PHP 函数用于此。

最后在validate.php中添加json_encode

mail($to, $subject, $message, $headers); 
echo json_encode(array('success'=>'true'));

JS代码

success: function(data){ 
     if(data.success == true){ 
       alert('success'); 
    } 

希望有效果

关于jQuery ajax 成功错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059489/

相关文章:

javascript - 如何使用上下键更改选择

javascript - 单击时使用 jQuery 和 AJAX 更改页面内容

javascript - 如何使用通过 AJAX 发送到 php 的结帐 token 创建 Stripe 收费

javascript - 从固定导航打开并滚动到 Accordion anchor

javascript - JQuery 悬停效果在第一次悬停时未启动

javascript - jQuery/CSS - 整个 Div 可点击,悬停时激活标签悬停状态

jquery - 悬停时显示近距离图像

javascript - 如何加载第二页作为覆盖层

php - 是否可以只包含 php Material 并在包含 php 文件时跳过 html 文本?

ajax - 与部分 View 并行的ASP.NET MVC AJAX调用返回错误消息