我试图对名为" sendMail.php"的文件执行ajax post请求。我不知道出了什么问题,我无法看到它。
js工作,它从输入中记录值,在chrome dev工具中我可以看到它被发送到PHP文件... chrome dev工具屏幕
我有点生疏,最近的PHP代码大约一年前。
下面你会找到我的PHP和js代码。
文件夹树(index.html是操作它的主文件):
PHP:
Critical error: wait for JVM process failed
--> Wrapper Started as Console
Launching a JVM...
Unable to execute Java command. Access is denied. (0x5)
Advice:
Access denied errors when attempting to launch the Java process are
usually caused by strict access permissions assigned to the directory
in which Java is installed.
JS:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
header('Content-Type: application/json');
$requestbody = file_get_contents('php://input','r');
$jsonbody = json_decode($requestbody, true);
$action = $jsonbody['action'];
switch($action){
case "sendMail":
$name = $requestbody->{'name'};
$gsm = $requestbody->{'gsm'};
$mail = $requestbody->{'mail'};
$msg = $requestbody->{'msg'};
$response = json_encode( '{"naam":"'.$name.'","gsm":"'.$gsm.'","mail":"'.$mail.'"}');
echo $response;
break;
}
}else{
http_response_code(405);
header('Content-Type: application/json');
$response = '{"error":"request method is not allowed."}';
echo($response);
}
?>
答案 0 :(得分:1)
Ajax中没有method
参数,因此请将其替换为type
。
请尝试以下代码:
$.ajax({
type:'POST',
url:'mail/sendMail.php',
dataType:'json',
data: {
"action":"sendMail",
"name": document.getElementById("mail_naam").value,
"gsm": document.getElementById("mail_gsm").value,
"mail":document.getElementById("mail_mail").value,
"msg": document.getElementById("mail_msg").value
}
}).done(function(data) {
console.log("succes");
console.log(data);
var result = JSON.parse(data);
console.log(result);
});
答案 1 :(得分:1)
我猜测问题是$requestbody
总是空的。
您应该使用$_POST
作为来源。
$requestbody = $_POST;
有点。
答案 2 :(得分:0)
我添加了这个,这有效:
PHP
if(empty($action) || $jsonbody){ $action = $_POST['action']; }
switch ($action) {
case "sendMail":
$name = $_POST["name"];
$gsm = $_POST['gsm'];
$mail = $_POST['mail'];
$msg = $_POST['msg'];
mailVraag($mail,$name,$msg);
mailVraag("achiel@protoware.be","protoware",$msg . "tel:".$gsm ."email: ".$mail);
$response = json_encode( '{"naam":"'.$name.'","gsm":"'.$gsm.'","mail":"'.$mail.'"}');
echo $response;
break;
}