js post x-www-form-urlencoded、form-url數據,Nodejs獲取x-www-form-urlencoded數據

1、post數據結構:

Form Data:

type:post
name:post發送url參數

 

2、js代碼實現

var xhr = new XMLHttpRequest();
xhr.open('post', 'http://localhost:3000/post', true);

xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(urlParams(data));

xhr.onload = function () {
  if (xhr.status === 200) {
    var text = xhr.responseText;
    if (success) success(JSON.parse(text));
  } else {
    if (error) error(xhr);
  }
};

 

 

 

3、後端獲取數據(Nodejs)

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

var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post('/urlPost', urlencodedParser, function(req, res) {
  res.header('Access-Control-Allow-Origin', '*');

  console.log('get application/x-www-form-urlencoded Params: ', req.body);

  res.json({result: 'success', data: req.body});
});

 

發佈了721 篇原創文章 · 獲贊 31 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章