Axios发布请求未向我的快速端点发送任何数据

时间:2019-02-07 11:16:42

标签: javascript jquery html node.js express

我对axios发布请求有问题。它根本没有开火。我尝试将axios代码放入IIFE中,但没有用。我尝试过手动配置axios发布请求,但这也没有用。我正在将您的完整代码发送给您。我还包括了身体解析器。

<script src="https://unpkg.com/axios/dist/axios.min.js">
  axios({
    method: 'POST',
    url: 'http://localhost:3000/enteruser_post',
    data: JSON.stringify({
      firstname: 'francis',
      lastname: 'jones'
    })
    config: {
      headers: {
        'Content-Type': 'application/json'
      }
    }
  }).then(function(response) {
    //handle success
    console.log(response);
  }).catch(function(response) {
    //handle error
    console.log(response);
  });
</script>
const express = require('express')
const app = express()
const querystring = require('querystring')
const bodyParser = require('body-parser')
const fs = require('fs')

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

app.get('/', (req, res) => {
  res.send('Hello World')
})

app.get('/user', (req, res) => {
  const html = fs.readFileSync(`${__dirname}/public/querystring.html`)
  res.send(`${html}`)
})

//querystring requests
app.post('/enteruser', (req, res) => { //listening for post request from form
  console.log(req)
  const firstname = req.body.firstname
  const lastname = req.body.lastname
  res.sendStatus(200)
  res.send(`${firstname} ${lastname}`)
}) //whenever we get an enteruser request, it's gonna parse ut through urlEncodeParser nad then send it as a request

//ajax request
app.get('/user_post', (req, res) => {
  const html = fs.readFileSync(`${__dirname}/public/jsonpost.html`)
  res.send(`${html}`)
})

app.post('/enteruser_post', (req, res) => {
  console.log(req.body)
  res.sendStatus(200)
})

const port = process.env.PORT || 3000
app.listen(port, '127.0.0.1')

0 个答案:

没有答案