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

138
Views
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 answers
Answer question

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 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!