Bharadwaj
Bharadwaj

Reputation: 11

How to send GET / POST method request parameters in Node.js

I am not able to get the data in the server side in nodejs I am new to node js ! I am using express ! The request when I saw in developer console it went like id=Bharadwaj&title=requestcheck !

<!DOCTYPE html>
<html>
<head>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<meta charset="utf-8" />
<title>Bharadwaj</title>
</head>
<body>
<div id="contact">
    <h1>Send an email</h1>
    <div>
      <input id="Bottle">
      <button id="name_one">Click me</button>
    </div>
</div>
</body>
<script>
$('#name_one').click(function() {
  var json='{"id":"SomeId","title":"SomeTitle"}';
  var obj=JSON.parse(json);
          $.ajax({
              url: "http://127.0.0.1:8080/putinto",
              type: "POST",
              dataType: "json",
              data: obj,
              contentType: "application/json",
              cache: false,
              timeout: 5000,
              complete: function() {
                //called when complete
                console.log('process complete');
              },

              success: function(data) {
                console.log(data);
                console.log('process sucess');
             },

              error: function() {
                console.log('process error');
              },
            });
      })
</script>
</html>

This is my server code .

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

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

app.get('/',(req,res)=>{
  res.send("hello express");
});

app.post('/putinto', function(req, res) {
  //I want to get req.id, req.title
  res.send("Hello World");
});
app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
});

Please help me . How can I get id and title in the server side and send it as response !

Working :

$('#name_one').click(function() {
  var json='{"id":"SomeId","title":"SomeTitle"}';
  var obj=JSON.parse(json);
          $.ajax({
              url: "http://localhost:8080/putinto",
              method: "POST",
              data: obj,
              cache: false,
              timeout: 5000,
              complete: function() {
                //called when complete
                console.log('process complete');
              },

              success: function(data) {
                console.log(data);
                console.log('process sucess');
             },

              error: function() {
                console.log('process error');
              },
            });
      })

This is working fine for me !

Upvotes: 0

Views: 293

Answers (1)

Harshal Yeole
Harshal Yeole

Reputation: 4983

At Client: Your code at client should be:

$.post('http://127.0.0.1:8080/putinto', { id: "54147512865132", title: "hello"}, 
    function(returnedData){
         console.log(returnedData);
}).fail(function(){
      console.log("error");
});

At server: You get the request data in req.body. Headers in req.headers

app.post('/putinto', function(req, res) {
//I want to get req.id, req.title
   console.log("Headersdata:", req.headers) // Here you will get JSON.
   console.log("params data:", req.params) // Here you will get JSON.
   console.log("JSON data:", req.body) // Here you will get JSON.
   res.send("Hello World");
});

Check terminal for the JSON object you sent from client.

Upvotes: 1

Related Questions