Set header using in React (ajax)

时光毁灭记忆、已成空白 提交于 2021-02-11 09:00:56

问题


try set header, in Ajax Request using axios

import React, { Component, PropTypes } from 'react'
import { Link, browserHistory } from 'react-router'
import { connect } from 'react-redux'
import axios from 'axios'

class Income extends Component {
  constructor(props) {
    super(props)

     this.state = {     
        };


  }


componentDidMount() {


 axios.post('http://139.196.141.166:8084/course/income/outline', {
      headers: { 'X-Authenticated-Userid': '15000500000@1' }
    })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
    }





render() {

        return (
            <div>
                dsdss
            </div>
        )
    }

But server returns me, that I not setup header. I setup it like in documentation, but header is not set.

There screen with error


回答1:


From what I can see, the method call looks like this (from docs):

axios#post(url[, data[, config]])

As you are posting your config object as second argument, its interpreted as data, not as config (headers is part of the config object)


Edit: Here is an example:

axios.request({
  url: 'http://139.196.141.166:8084/course/income/outline',
  method: 'post',
  headers: { 'X-Authenticated-Userid': '15000500000@1' }
})

Note that i switched over from .post() to .request() which only takes a config object. If you still want to use the .post() call, try:

axios.post(url, {}, { headers: ... })


来源:https://stackoverflow.com/questions/41790854/set-header-using-in-react-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!