1

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

enter image description here

1 Answer 1

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/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: ... })
Sign up to request clarification or add additional context in comments.

4 Comments

Can you change it with my code? don't understand how to change
@ЕгорКротенко sure... added 2 examples
How do post call, with first example? I mean last part where I can make console.log
@ЕгорКротенко you would just use the returned Promise the same way (with .then() and .catch())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.