I want to send a POST request through AJAX from a website to an express server when a button is clicked. When I run the server on the same port as the website (localhost:8080), the website no longer renders and I receive a message that states, "Cannot GET /mogle/income.php," "mogle/income.php" being the webpage I want to send the request from. When I run the server on a different port (localhost:8000), the AJAX POST call no longer works. From my current understanding, you can only send requests to different ports using JSONP, but JSONP can only work with GET requests, and I need to use POST.
I've tried putting a app.get function in my server.js file (see code snippet below) to address the webpage not rendering when the server runs on the same port as the website, but when I go to "localhost:8080/mogle/income.php", the file downloads instead of rendering, which I believe is because it's a PHP file and not an HTML file. However, even if this were to work, would I then have to make an app.get statement for every single other webpage (homepage.php, login.php, etc.) on the website in the server.js file?
app.get("/mogle/income.php", (req, res) => {
// "../../" because the server file is in two folders
res.sendFile(path.join(__dirname, "../../income.php"));
});
app.post("/path", async (req, res) => {
// Process response
});What is the convention for sending requests (both POST and GET) from websites to servers in terms of ports?
Aucun commentaire:
Enregistrer un commentaire