簡體   English   中英

反應,axios,內容類型

[英]React, axios, content-type

我已經搜索了很多,但沒有一個解決方案找到工作:Cannot send content-type by axios。 但是如果我使用 postman 攔截器並“發送”由 axios 生成的請求,這次它可以正常工作:node.js / express 服務器正常接收請求和正文解析器正常工作!

反應方面:

const API_URL = "http://localhost:8800/auth/";

const headers = {
  accept: 'application/json, text/plain, */*',
  'content-type': 'application/json;charset=UTF-8'
};

class AuthService {
  register(pseudo, email, password) {
    return axios.post(API_URL + "signup/",
      { pseudo, email, password },
      { headers: headers})
    .then(response => {
      if (response.data.accessToken) {
        localStorage.setItem("user", JSON.stringify(response.data));
      }
      return response.data;
    });
  }

服務器端

const app = express();
app.use(function (req, res, next) {
    console.log( req.headers);
    next();
}); 
app.use( bodyParser.urlencoded({ extended: true }), bodyParser.json()); 

Usually when I use axios I send the headers in a config variable like this and I stringify the body so it sends as JSON object and not a JS object.

const config = {
      headers: {
        'Content-Type': 'application/json',
      },
    };

    const body = JSON.stringify({arguments});
    try {
      const res = await axios.post(/url, body, config);

...

這是文檔的鏈接,以獲取更多關於它的信息:

https://github.com/axios/axios

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM