jQuery ajax成功错误

elc*_*nrs 13 ajax jquery

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

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!');
  }
});
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

我正在设置,that因为我使用模态窗口插件来显示表单,这样我得到了正确的范围.

主要的问题是当我点击提交时,电子邮件进入我的收件箱就好了,但它总是执行错误功能而不是成功的.

这让我疯了,我无处运气地寻找答案.

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

我使用Chrome开发者工具检查了响应标头,我得到200 OK了回复.问题是什么?

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

Pat*_*dhi 7

你没有提供你的validate.php代码,所以我很困惑.当邮件成功时,您必须以JSON格式传递数据.你可以使用json_encode(); PHP的功能.

json_encdoe最后添加validate.php

mail($to, $subject, $message, $headers); 
echo json_encode(array('success'=>'true'));
Run Code Online (Sandbox Code Playgroud)

JS代码

success: function(data){ 
     if(data.success == true){ 
       alert('success'); 
    } 
Run Code Online (Sandbox Code Playgroud)

希望它有效