0

I am using post method in nodejs

Well it is working fine when I run it using postman

but when I run it in my browsers it shows error

Cannot GET /listUsers and listUsers:1 GET http://localhost:8081/listUsers 404 (Not Found) this

here is my node js code

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.post('/listUsers', function (req, res) {
        var f = parseInt(req.body.f);
    console.log("hello" + f);
   var l = parseInt(req.body.l);
    var sum = Number(f + l);
    res.send('The sum is: ' + Number(sum));
})
var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port
   console.log("Example app listening at http://%s:%s", host, port)
})

here is my jquery code from where I am sending data

usid();
function usid(med){
  var f = "my new name";
  $.ajax({
    crossDomain: true,
    url:"http://localhost:8081/listUsers",
    method:"POST",
    data:{med,f:f},
    success:function(data,status){
      console.log("send");
    }
  })
}

can I get where I am doing mistake?

deepak pal
  • 17
  • 5
  • 1
    You can only send GET requests with browsers. – Murat Colyaran Jun 13 '21 at 19:14
  • 1
    For testing `post, delete, put or patch` use `cuRL` or `postman`. – snd Jun 13 '21 at 19:15
  • Here is an [Example](https://stackoverflow.com/questions/7172784/how-do-i-post-json-data-with-curl) – snd Jun 13 '21 at 19:16
  • but in get method I cannot send data and I am using webrtc and I am connecting the nodejs with php through ajax so I have to send and receive data from my nodejs so if I cannot use post then can you suggest me any other http method to pass or receive data in browsers??@murat colyaran,@sefa – deepak pal Jun 13 '21 at 19:43
  • What data are you sending in you listUsers? Do not see any in your code. Check how query parameters work to send some data with GET. – Tushar Shahi Jun 13 '21 at 20:12
  • Actully I am sending the data from Jquery ajax to nodejs but its not working @Tushar shahi – deepak pal Jun 14 '21 at 03:47
  • Please show your jQuery code too. – Tushar Shahi Jun 14 '21 at 04:04
  • I have update my question and added the jquery code too in it @TusharShahi – deepak pal Jun 14 '21 at 18:59
  • what is your jQuery version ? before 1.9 there was not any "method" properties in $.ajax, instead it was "type" you could also try use $.post – r043v Jun 14 '21 at 19:18
  • my jquery version is 2.2.4 @r043v – deepak pal Jun 14 '21 at 19:24
  • your error message is clear that it's a get request, you may missform your $.ajax give a try at $.post : $.post("/listUsers",{hello:"world"},function(data){ console.log(data); }); – r043v Jun 14 '21 at 19:37
  • in my own test all work fine, using $.ajax like you with your options and same url formatting or directly with $.post, what is this "med" variable used to construct your post values ? – r043v Jun 14 '21 at 19:55
  • I have tried this too but its not working ?@r043v – deepak pal Jun 15 '21 at 06:25
  • data:{med,f:f} this is not valid – r043v Jun 15 '21 at 11:01

1 Answers1

1

You are not submitting any form/storing any resource. What you are trying should ideally be done with GET.

Reasons it is not working with browser: Visiting a page by changing url is a GET method. You do not have a GET method defined in your app for this route. Only one POST method.

What you can do: Change POST to GET both in server and your AJAX. Change both methods accordingly. You will then have to pass query params(google something and everything after the question mark is how query Params work).

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39