Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Cómo enviar y recibir correctamente solicitudes "POST" hacia y desde un servidor

Estoy tratando de hacer un servidor para un sitio web. El servidor funciona perfectamente y puede recibir y enviar datos con normalidad. He alojado el servidor e hice una demostración del lado del cliente en Replit para probar el flujo. Pero de alguna manera, la respuesta no se recibe o envía correctamente, o el servidor no funciona. (Para el código del lado del cliente, estoy usando JQuery)

 // Client Side Code
const url = "My url";
const data = {
 "file": "lol.html"
}

function start() {
 console.log("test 1")
 $.post(url, data, function(data, status) {
 console.log(data + "is data & status is " + status);
 });
}
 // Server-side code
var http = require('http'); // Import Node.js core module
var fs = require('fs'); // File System
var url = require('url');

var server = http.createServer(function(req, res) { //create web server
 if (req.url == '/') { //check the URL of the current request

 res.writeHead(200, { 'Content-Type': 'application/json' });
 res.write(JSON.stringify({ message: "This is the Backend server for my website" }));
 res.end();

 }
 else if (req.url == "/ping") {

 res.writeHead(200, { 'Content-Type': 'application/json' });
 res.write(JSON.stringify({ message: "Pong!" }));
 res.end();

 }
 else if (req.url.startsWith("/read")) {

 var q = url.parse(req.url, true);
 var qdata = q.query;
 fs.readFile(String(qdata.file), function(err, data) {
 if (err) {
 res.writeHead(404, { 'Content-Type': 'text/html' });
 return res.end("404 Not Found");
 }
 res.writeHead(200, { 'Content-Type': 'text/html' });
 res.write(data);
 return res.end();
 });
 }
 else if (req.url.startsWith("/get")) {

 var q = url.parse(req.url, true);
 var qdata = q.query;
 fs.readFile(String(qdata.file), function(err, data) {
 if (err) {
 res.writeHead(404, { 'Content-Type': 'text/html' });
 return res.end("404 Not Found");
 }
 res.writeHead(200, { 'Content-Type': 'text/html' });
 res.write(data);
 return res.end();
 });
 }
 else {
 res.end('Invalid Request!');
 }

});

server.listen(5000); //6 - listen for any incoming requests

console.log('Node.js web server at port 5000 is running..')

¿Alguien puede decirme cómo realizar esto correctamente?

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Utilice axios en lugar de tecnologías antiguas de obtención de datos. Es un cliente HTTP basado en promesas para el navegador y node.js.

Realización de una solicitud POST

 axios.post('/YOUR_FULL_API_URL', {
 data1: 'value1',
 data2: 'value2'
})
.then(function (response) {
 console.log(response);
})
.catch(function (error) {
 console.log(error);
});

Documentación completa: https://axios-http.com/
Léame del paquete NPM: https://www.npmjs.com/package/axios

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!