我试图在客户端获得成功,但我做不到。如果我把
error:function(data){
console.log(data);
console.log('error');
}ajax中的这段代码请求this捕获了一些东西,但它不应该出错。
我试了那么多东西,但还是找不到解决方案。
这里是我在客户端的ajax请求;
<script>
$(document).on("submit", "#request-form", function(event){ //request-form id li form post edildiğinde
event.preventDefault();
var serialized = $(this).serialize();
if(serialized.indexOf('=&') > -1 || serialized.substr(serialized.length - 1) == '='){ //formda boş yer var ise
alert("Fill in all fields");
}else{
$.ajax({
url: "http://127.0.0.1/rent_website/mail-sender/mail.php", //"https://stanstedcab.co.uk/project/mail-sender/mail.php",
type: "POST",
data: serialized,
dataType: "json",
function(data, status) {
console.log('function works');
if (data.success) {
console.log(data);
console.log('Başarılı');
} else {
console.log('else', data)
}
},
});
}
});
</script>和这里的后端;
<?php
$response = array();
if ($_POST){
if(isset($_POST["your-pickup"]) && isset($_POST["your-drop"]) && isset($_POST["Vehicle"]) &&
isset($_POST["meeting-time"]) && isset($_POST["your-name"]) && isset($_POST["your-phone"]) &&
isset($_POST["your-email"]) && isset($_POST["your-message"])) {
//here some mail settings
if($mail->Send()){
$message = "Email sent";
$response["success"] = true;
$response["message"] = $message;
echo json_encode($response);
return $response;
} else {
$response = array('result' => 'Email couldn\'t sent', 'success' => false);
echo json_encode($response);
return $response;
}
}else{
$response = array('result' => 'Fill all fields.', 'success' => false);
echo json_encode($response);
return $response;
}
}
?>发布于 2020-09-01 18:45:49
我试着用下面示例中所示的方式编写Javascript:https://api.jquery.com/jquery.ajax/
<script>
$(document).on("submit", "#request-form", function(event){ //request-form id li form post edildiğinde
event.preventDefault();
var serialized = $(this).serialize();
if(serialized.indexOf('=&') > -1 || serialized.substr(serialized.length - 1) == '='){ //formda boş yer var ise
alert("Fill in all fields");
}else{
$.ajax({
url: "http://127.0.0.1/rent_website/mail-sender/mail.php", //"https://stanstedcab.co.uk/project/mail-sender/mail.php",
type: "POST",
data: serialized,
dataType: "json"
}).done(function(data) {
console.log('function works');
if (data.success) {
console.log(data);
console.log('Başarılı');
} else {
console.log('else', data)
}
});
}
});
</script>https://stackoverflow.com/questions/63686030
复制相似问题