I am trying to send string data to a php script on localhost:8080 to be saved, but the Javascript function cannot find the php file and gives a 404 error.
This is the Javascript:
var data = new FormData();
data.append("data" , myStringData);
var xhr = new XMLHttpRequest();
xhr.open('POST', "http://localhost:8080/Source/ServerInputManager.php", true);
xhr.send(data);
This is the ServerInputManager.php script:
<?php
if (!empty($_POST['data'])) {
$data = $_POST['data'];
$fname = "NewFile.txt";
$file = fopen($fname, 'w'); // Creates new file
fwrite($file, $data);
fclose($file);
}
?>
I know the php file exists because I can download it from that URL in chrome. I can also read the contents with a GET request using that URL. But whenever I try to use POST, it gives a 404 error. I am not using any php libraries, but I am using node to run a server.js script.
This is the file structure:
|- index.html
|- index.css
|- server.js (ran by node)
+- Source
|- InputManager.js (contains the javascript code)
+- ServerInputManager.php (contains the php code)
I've also tried using these directories:
http://localhost:8080/Source/ServerInputManager.php
http://localhost:8080/ServerInputManager.php
localhost:8080/Source/ServerInputManager.php
localhost:8080/ServerInputManager.php
/Source/ServerInputManager.php
Source/ServerInputManager.php
/ServerInputManager.php
ServerInputManager.php
But they all gave 404 errors. What am I doing wrong?
app.use()orapp.get()andapp.post()in yourserver.jsfile?/Source/ServerInputManager.phpin your Express router setup.