0
Server-side code
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
const port = 8080;
app.use(bodyParser.urlencoded({extended: false}));
app.use((req, res, next)=>{
res.setHeader("Access-Control-Allow-Origin", "http://localhost");
res.setHeader("Access-Control-Allow-Methods", "GET, POST");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type");
res.setHeader("Access-Control-Allow-Credentials", true);
next();
});
app.post("/api", (req, res)=>{
res.set("Content-Type", "application/json");
res.set("Accept-Charset", "utf-8");
const msgData = {
getMsg : req.body.phrase,
geMsgDUC : decodeURIComponent(req.body.phrase),
};
console.log(JSON.stringify(msgData));
res.send(JSON.stringify(msgData.geMsgDUC));
});
app.listen(port, (err)=>{
if (err){
throw err;
}
console.log("Server started on http://localhost:"+port);
});
Request on the Client-Side
fetch("http://localhost:8080/api", {
method: 'POST',
headers: {'Content-Type':'application/x-www-form-urlencoded'},
body: "phrase="+encodeURIComponent("~(+´g°i.v=+[ÿqéºqiyk'ïìù;7ú´=%dz")
}).then(function(response){
return response.json();
}).then(function(json){
console.log(json);
});
am having an error as return on server side:
URIError: URI malformed
at decodeURIComponent (<anonymous>).
OBS: the string is encoded and decoded normally in the browser console.
Did you read the error? The server gave back to the client what happened.
– Jefferson Quesado
I read yes friend, I am starting with js on the server side, could you explain me why this error? since on the client side(browser) does not happen?
– Jefter Rocha
Read on mdn about the error that appears to you. In your case some information to be coded is not valid
– Isac
I read it, friend, 3 days ago, and remember that this error only happens in the server-side, if I find the information on the client side everything happens normally understood?
– Jefter Rocha