我最近一直在尝试使用Jquery的ajax函数从php获取信息发送请求工作正常但接收信息却没有。我很感激你的帮助。
Jquery的:
body
php:
$.ajax({
url: 'sendEmail.php',
type: 'post', // performing a POST request
data: values,
dataType: 'json',
success: function(result)
{
console.log("check");//code does not come here
if(result=="Success"){
alert("Your message has been sent. I will get in touch with you soon");
}
else{
alert("Umm.. The meesage was not sent :(. You can still contact me on myemail@gmail.com");
}
}
});
答案 0 :(得分:1)
dataType: 'json',
需要PHP的json回复,在你的php代码中添加标题。
<?php
header('Content-Type: application/json');
$recipient = 'myemail@gmail.com';
$subject = "Portfolio Message";
$fromName = stripslashes($_POST['Name']);
$fromEmail= stripcslashes($_POST['Email']);
$msg = "Message from: $fromMessage\nEmail: $fromEmail\n\n".stripslashes($_POST['Message']);
if (mail($recipient, $subject, $msg)){
echo json_encode("Success");
}
else {
echo json_encode("Fail");
}
?>
答案 1 :(得分:1)
问题:
响应类型与ajax期望的dataType不匹配
要解决您的两个选择:
这两个中的任何一个都会解决你的问题发送json的例子就是在json_encode
中编码响应