jQuery ajax 成功错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9059489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax success error
提问by elclanrs
I'm trying to submit a form witch sends an email:
我正在尝试提交表单女巫发送的电子邮件:
JS
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
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 protected]';
$subject = 'Subject';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\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.
我设置that
是因为我使用模态窗口插件来显示表单,这样我就可以获得正确的范围。
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...
我收到了几次成功回调,但现在它不再起作用了,我不知道为什么。我收到邮件就好了...
I checked the response headers with Chrome Developer tools and I get 200 OK
so it's getting back. What is the problem?
我使用 Chrome 开发人员工具检查了响应标头,我明白了200 OK
,它又回来了。问题是什么?
EDIT: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...
编辑:嗯,我在尝试了 5 个小时后放弃了今天。我明天可能会回来尝试其他事情。我complete
现在就用,效果很好。我认为这可能与服务器有关。客户端正在使用 IIS...
采纳答案by Pathik Gandhi
You did not provide your validate.php
code so I'm confused. You have to pass the data in JSON Format when when mail is success.
You can use json_encode();PHP function for that.
你没有提供你的validate.php
代码,所以我很困惑。当邮件成功时,您必须以 JSON 格式传递数据。您可以使用json_encode(); PHP 函数。
Add json_encdoe
in validate.php in last
json_encdoe
最后添加validate.php
mail($to, $subject, $message, $headers);
echo json_encode(array('success'=>'true'));
JS Code
JS代码
success: function(data){
if(data.success == true){
alert('success');
}
Hope it works
希望它有效
回答by Dima Koderov
Try to set response dataType property directly:
尝试直接设置响应数据类型属性:
dataType: 'text'
and put
并放
die('');
in the end of your php file. You've got error callback cause jquery cannot parse your response. In anyway, you may use a "complete:" callback, just to make sure your request has been processed.
在你的 php 文件的末尾。您有错误回调,因为 jquery 无法解析您的响应。无论如何,您可以使用“完成:”回调,以确保您的请求已被处理。
回答by Simon Forsberg
I had the same problem;
我有同样的问题;
textStatus = 'error'
errorThrown = (empty)
xhr.status = 0
That fits my problem exactly. It turns out that when I was loading the HTML-page from my own computer this problem existed, but when I loaded the HTML-page from my webserver it went alright. Then I tried to upload it to another domain, and again the same error occoured. Seems to be a cross-domain problem. (in my case at least)
这完全符合我的问题。事实证明,当我从我自己的计算机加载 HTML 页面时,这个问题存在,但是当我从我的网络服务器加载 HTML 页面时,一切正常。然后我尝试将其上传到另一个域,再次出现相同的错误。好像是跨域问题。(至少在我的情况下)
I have tried calling it this way also:
我也试过这样称呼它:
var request = $.ajax({
url: "http://crossdomain.url.net/somefile.php", dataType: "text",
crossDomain: true,
xhrFields: {
withCredentials: true
}
});
but without success.
但没有成功。
This post solved it for me:jQuery AJAX cross domain
这篇文章为我解决了这个问题:jQuery AJAX cross domain