如何在Node.js中发送GET / POST方法请求参数

时间:2018-05-27 10:30:47

标签: node.js express

我无法在nodejs中获取服务器端的数据我是节点js的新手!我正在使用快递!我在开发者控制台中看到的请求就像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>

这是我的服务器代码。

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/');
});

请帮帮我。如何在服务器端获取id和title并将其作为响应发送!

工作:

$('#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');
              },
            });
      })

这对我来说很好用!

1 个答案:

答案 0 :(得分:1)

客户: 您在客户端的代码应为:

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

在服务器上: 您可以在req.body中获取请求数据。 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");
});

在终端上查看您从客户端发送的JSON object