最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set header using in React (ajax) - Stack Overflow

programmeradmin2浏览0评论

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 Ine extends Component {
  constructor(props) {
    super(props)

     this.state = {     
        };


  }


ponentDidMount() {


 axios.post('http://139.196.141.166:8084/course/ine/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

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 Ine extends Component {
  constructor(props) {
    super(props)

     this.state = {     
        };


  }


ponentDidMount() {


 axios.post('http://139.196.141.166:8084/course/ine/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

Share Improve this question asked Jan 22, 2017 at 12:25 Егор КротенкоЕгор Кротенко 8183 gold badges14 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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/ine/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: ... })
发布评论

评论列表(0)

  1. 暂无评论