问题
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