jQuery的阿贾克斯成功错误 [英] jQuery ajax success error

查看:128
本文介绍了jQuery的阿贾克斯成功错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提交表单女巫发送电子邮件:

I'm trying to submit a form witch sends an email:

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);

我设置,因为我使用的是模态窗口插件显示的形式,这样一来我得到的权利范围。

I'm setting that because I'm using a modal window plugin to display the form, this way I get the right scope.

主要的问题是,当我点击提交电子邮件获取我的收件箱就好了,但它始终执行错误的功能,而不是成功的。

The main problem is that when I click submit the email gets in my inbox just fine but it always executes the error function and not the success one.

这是我发疯,我到处找的答案,没有运气。

This is driving me nuts, I looked EVERYWHERE for answers with no luck.

我得到了成功的回调几次,但现在它已经不工作,我不知道为什么。我收到的电子邮件就好了......

I got the success callback a couple times but now it doesn't work anymore and I don't know why. I'm getting the email just fine...

我检查了Chrome开发工具的响应头和我得到 200 OK 所以它找回。这是什么问题?

I checked the response headers with Chrome Developer tools and I get 200 OK so it's getting back. What is the problem?

编辑:好吧,我放弃了今天5小时后尝试。我可以明天再来,并尝试其他的东西。我就用完整现在,它工作正常。我想这可能与服务器。客户端使用IIS ...

Well, I give up for today after 5 hours trying. I might come back tomorrow and try other things. I'll just use complete for now, which works fine. I'm thinking it could be related to the server. The client is using IIS...

推荐答案

您还没有提供您validate.php code。所以这是混淆了我。但你要传递的数据以JSON格式时邮件时是成功的。 您可以使用 json_en code(); PHP函数,该函数。

You have not provided you validate.php code. so it is confusing me. but you have to pass the data in JSON Format when when mail is success. You can use json_encode(); PHP function for that.

添加jsonencdoe在validate.php去年

add jsonencdoe in validate.php in last

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

JS code

JS Code

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

希望工程

这篇关于jQuery的阿贾克斯成功错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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