K
K
kr_ilya2019-05-10 09:20:34
Express.js
kr_ilya, 2019-05-10 09:20:34

What am I doing wrong when accessing the server?

I broke my head, I don’t understand ... I
execute a request from the vue axios post component
The server processes it like this:

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.post('/punycode', (req, res) => {
  res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
    res.header("Access-Control-Allow-Headers", "Content-Type");
  console.log(req.body);
});


If I send data like this:
var postData = {
          type: 'text',
          pdata: this.text
        }
const str = JSON.stringify(postData);
//передается str

From the server to the console in this case I get this:
[Object: null prototype] { '{"type":"text","pdata":"Введите текст"}': '' }

And if I just pass
var postData = {
          type: 'text',
          pdata: this.text
        }

Then already in the browser console I get this:
5cd5169f336bd640878554.png
What's wrong?
Axios is done like this:
api.js file
import axios from 'axios'
var domen = 'localhost'
export default() => {
  return axios.create({
    baseURL: '//' + domen + ':3000'
  })
}

service.js file
import Api from '@/services/Api'

export default {

  Pr (params) {
    return Api().post('abc', params)
  },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-05-10
@kr_ilya

Missing
app.options('/punycode', (req, res) => {
})
More info in mdn, it's explained in great detail.
There is a cors module for express, it does it all by itself. Just like the body parser is connected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question