I am running fetch as a way to run php files from javascript. Specifically, the following code is executed
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<form name="form" id="form">
<input type="text" id="text">
<button type="button" onclick="push()">push</button>
</form>
<script>
async function push(){
var form = document.form;
console.log(form.text.value);
await fetch('test.php?text='+ form.text.value)
.then(res=> {return res.text();})
.then(res => {
console.log(res);
var result = JSON.parse(res);
console.log(result.result);
console.log(result.msg);
});
}
</script>
</body>
</html>
<?php
if(isset($_GET['text']) && $_GET['text']!=""){
$result = "OK";
$msg = $_GET['text'];
}else{
$result = "NG";
$msg = "message";
}
$result = json_encode(
array(
"result" => $result,
"msg" => $msg,
)
);
echo $result;
return $result;
?>
This works fine on windows, but does not work on linux. The entire php script is returned in console.log(res). Do you know why?
The linux environment uses the following
OS: Debian GNU/Linux 10 \n \l
PHP: PHP 7.3.31-1~deb10u1 (cli) (built: Oct 24 2021 15:18:08) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.31, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.31-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies
Do I need some kind of spell to make it recognize the php file?