在React(ajax)中使用设置头 [英] Set header using in React (ajax)

查看:225
本文介绍了在React(ajax)中使用设置头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用axios的Ajax请求中尝试设置标头

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.

出现错误屏幕

推荐答案

据我所知,方法调用如下所示(

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

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

在将 config 对象作为第二个参数发布时,其解释为 data ,而不是 config ( header 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)

这是一个示例:

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

请注意,我从.post()切换到.request(),后者仅使用配置对象.如果您仍然想使用.post()调用,请尝试:

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: ... })

这篇关于在React(ajax)中使用设置头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆