Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
expressjs is not returning any response to html

while submitting a form without using ajax I could see "message sent successfully" in my http://localhost/ss (working fine as it should)

But while submitting a form using ajax $.post() response is not receiving to $.post() method. I couldn't find any reason..

Please note: same code works fine with php

index.html

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script>
    $(document).ready(function() {
      $("#buttons").click(function() {
        $.post("http://localhost:3000/ss", {
          sendemail: $("#email").val(),
          sendname: $("#UserName").val(),
          sendpass: $("#Password").val()
        }, function(res) {
          alert(res);
        });
      });
    });
  </script>

  <body>

    <div class="forms">
      <form>
        <div class="formdiv">
          <label>User Email</label>
          <input type="email" id="email" name="email" />
        </div>
        <div class="formdiv">
          <label>User Name</label>
          <input type="text" name="UserName" id="UserName" />
        </div>
        <div class="formdiv">
          <label>User Password</label>
          <input type="password" name="Password" id="Password" />
        </div>
      </form>
      <div style="background:green;padding:15px;" id="buttons">send</div>
    </div>
  </body>

</html>

parse.js

var express = require("express");
var bodyParser = require("body-parser");
var app = express();

app.listen(3000, function(req, res)
{
  console.log("express is listening to port 3000");
});

app.use(bodyParser.urlencoded({
  extended: true
}))

app.use(bodyParser.json());

app.get("/", function(req, res)
{
  res.send("hai");
});

app.post("/ss", function(req, res)
{
  var ss = req.body.sendemail;
  if (ss != undefined)
  {
    console.log(ss);
    res.send("message sent successfully");
  }
  else
  {
    res.send("error occurred");
  }
});

console prints user's email address "The only problem is response to html"

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Code looks okey by itself, although there's one issue, with (i suspect) the way you use it. You're not allowing for cross-origin sharing. In other words, if you'd try to run this code on another domain, you'd receive a CORS error, as server refuses to respond to the client.

Therefore, I suspect you're loading the .html file either:

  • As local, html file.
  • Are running it from different domain

both of those would (and are) returning mentioned above error. That's why you're not receiving the response, so you're not seeing the alert message.

In order to bypass the issue, you can either:

  • Enable CORS support
  • Render HTML file through the server (so request will be coming from the same domain).

Example here:

var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var path = require("path");

...

app.get("/", function(req, res)
{
  res.sendFile(path.join(__dirname + '/index.html'));
});

app.post("/signup", function(req, res)
{
  var email = req.body.email;

  if (!email)
  {
    return res.json({
        message: "error occurred"
    });
  }

  res.json({
    success: true;
  });
});
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda