ottdevott
ottdevott

Reputation: 25

Sending a post request with express

I am experimenting with ajax, cors and post requests to a mysql-server. I am trying to post a color code received from html but it doesnt seem to work.

HTML-form:

<form method="POST" action="http://localhost:3000/">
<label for="knapp"></label><br>
<input type="range" id="knapp" onchange="changecolor()">
<label for="submit"></label><br>
<input id="submit" value="Save" type="submit">

<label for="text"></label><br>
<input type="text" id="text" readonly>

<label for="colorcode"></label><br>
<input type="text" id="colorcode" readonly>
</form>

Node:

const express = require('express')
const app = express();
const cors = require("cors")
app.use(cors({
    origin: "*"
}))

app.get("/", (req, res) => {
    console.log("get")
})

app.post("/", (req, res) => {
    var mysql = require('mysql');
    var con = mysql.createConnection({
        host: "secret",
        user: "secret",
        password: "secret",
        port: "secret"
    });
    con.query(`insert into studocliu_se_db.Kalender values(` + 950 + `)`, (err, res) => {
        return console.log(res)
    })
    con.end()
    res.end();
})

I know the sql-connection is working, it is the POST-operation that doesn't go through. I am getting this console-message as well as the header below: https://i.postimg.cc/vTgW8wvs/Untitled4.png

The status of the requests is "(failed) net::ERR"

I do not know what i might be doing wrong.

Upvotes: 1

Views: 281

Answers (1)

Arthur LR
Arthur LR

Reputation: 48

Could you try to replace res.end() with res.send() : https://expressjs.com/en/api.html#res.send

This way you should be able to return a response to the client.

Upvotes: 1

Related Questions