我不明白为什么这个ajax请求返回错误,即使错误状态是OK(200),有什么想法吗?似乎我错过了一些非常明显的东西,但我一直在试图寻找答案而撕裂自己!我是ajax的新手,因此没什么经验!谢谢你的帮助。
这是我的ajax请求:
var dataString = $('#cform').serialize();
$.ajax({
type: "POST",
url: 'contact.php',
data: dataString,
dataType: 'json',
success: function (data) {
if (data.success == 0) {
var errors = '<ul><li>';
if (data.name_msg != '')
errors += data.name_msg + '</li>';
if (data.email_msg != '')
errors += '<li>' + data.email_msg + '</li>';
if (data.godaddyemail_msg != '')
errors += '<li>' + data.godaddyemail_msg + '</li>';
if (data.godaddyuser_msg != '')
errors += '<li>' + data.godaddyuser_msg + '</li>';
$("div#output").removeClass('alert-success').addClass('alert-error').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p> Could not complete your request. See the errors below!' + errors + '</p>');
}
else if (data.success == 1) {
$("div#output").removeClass('alert-error').addClass('alert-success').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p>You message has been sent successfully!</p>');
}
},
error: function (error) {
$("div#output").removeClass('alert-success').addClass('alert-error').show().html('<button type="button" class="close" data-dismiss="alert">x</button><p> Could not complete your request. The Function returned an error!</p>' + error.statusText); }
});
return false;
}
这是contact.php(请求发送到的地方):
<?php
$send_email_to = "My Email";
$message = 'message';
$subject = 'subject';
function send_email($name,$email,$godaddyusername,$godaddyemail)
{
global $send_email_to;
if($message=='message')$message='';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$email. "\r\n";
$message = "<strong>Email = </strong>".$email."<br>";
$message .= "<strong>Name = </strong>".$name."<br>";
$message .= "<strong>GoDaddy Username = </strong>".$godaddyusername."<br>";
$message .= "<strong>GoDaddy Email = </strong>".$godaddyemail."<br>";
mail($send_email_to, $subject, $message,$headers);
return true;
}
function validate($name,$email,$godaddyusername,$godaddyemail)
{
$return_array = array();
$return_array['success'] = '1';
$return_array['name_msg'] = '';
$return_array['email_msg'] = '';
$return_array['godaddyuser_msg'] = '';
$return_array['godaddyemail_msg'] = '';
if($email == '')
{
$return_array['success'] = '0';
$return_array['email_msg'] = 'Email is required';
}
else
{
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$return_array['success'] = '0';
$return_array['email_msg'] = 'Enter valid emmail.';
}
}
if($name == '')
{
$return_array['success'] = '0';
$return_array['name_msg'] = 'Name is required';
}
else
{
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$return_array['success'] = '0';
$return_array['name_msg'] = 'Enter valid Name.';
}
}
if($godaddyusername == '')
{
$return_array['success'] = '0';
$return_array['godaddyuser_msg'] = 'GoDaddy Username is required';
}
if($godaddyemail == '')
{
$return_array['success'] = '0';
$return_array['godaddyemail_msg'] = 'GoDaddy Email is required';
}
else
{
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$godaddyemail)) {
$return_array['success'] = '0';
$return_array['godaddyemail_msg'] = 'Enter a valid GoDaddy email.';
}
}
return $return_array;
}
$name = $_POST['name'];
$email = $_POST['email'];
$godaddyemail = $_POST['gde'];
$godaddyusername = $_POST['gdu'];
$return_array = validate($name,$email,$godaddyusername,$godaddyemail);
if($return_array['success'] == '1')
{
send_email($name,$email,$godaddyusername,$godaddyemail);
}
header('Content-type: text/json');
echo json_encode($return_array);
die();
?>
答案 0 :(得分:0)
那是因为你的send_email()
函数有两个未定义的变量。
此处您有 if
声明..
if($message=='message')$message=''; // $message is not available inside this function.
在这里
mail($send_email_to, $subject, $message,$headers); //$subject is also undefined.