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因为我使用模态窗口插件来显示表单,这样我得到了正确的范围。

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

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

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

我使用Chrome开发者工具检查了响应标头,我得到了200 OK所以它又回来了。 问题是什么?

编辑:好吧,我尝试了5个小时后今天放弃了。 我明天可能会回来尝试其他事情。 我现在只使用complete ,工作正常。 我认为它可能与服务器有关。 客户端正在使用IIS …

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

最后在validate.php中添加json_encdoe

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

JS代码

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

希望它有效

尝试直接设置响应dataType属性:

 dataType: 'text' 

并把

 die(''); 

在你的PHP文件的最后。 您有错误回调导致jquery无法解析您的响应。 无论如何,您可以使用“完成:”回调,以确保您的请求已被处理。

我有同样的问题;

 textStatus = 'error' errorThrown = (empty) xhr.status = 0 

这恰好符合我的问题。 事实certificate,当我从我自己的计算机加载HTML页面时,存在这个问题,但是当我从我的网络服务器加载HTML页面时,它就没问题了。 然后我尝试将其上传到另一个域,并再次出现相同的错误。 似乎是一个跨域问题。 (至少在我的情况下)

我也试过这样调用它:

 var request = $.ajax({ url: "http://crossdomain.url.net/somefile.php", dataType: "text", crossDomain: true, xhrFields: { withCredentials: true } }); 

但没有成功。

这篇post为我解决了: jQuery AJAX跨域