我无法在AXIOS中发出GET请求?

有趣的小丑

为什么我收到此错误CORS政策。我在localhost:3000后台,后端是localhost:5000通过向GET请求从后端服务器获取数据localhost:5000/api/users

userAction.js

import { FETCH_USERS } from './types';
import axios from 'axios';

export const fetchUsers = () => dispatch => {
    axios.get(`localhost:5000/api/users`)
    .then( users => 
        dispatch({
            type: FETCH_USERS,
            payload: users
        })
    )
    .catch( error => {
        console.log(error);
    });
};

userReducer.js:

import { FETCH_USERS } from '../actions/types';

const initialState = {
    items: []
}

export default function (state = initialState, action) {
    switch (action.type) {
        case FETCH_USERS:
            return {
                ...state,
                items: action.payload
            };
        default:
            return state;
    }
}

现在,我正在获取GET请求,localhost:5000/api/users并且我在客户端localhost:3000上添加了以下代码,server.js但仍显示相同的错误。

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

屏幕截图:

在此处输入图片说明

特朗堂

我上个月遇到了这个问题,这是我要解决的问题。对于后端accpet跨域,请尝试以下操作(我的后端是nodejs):

app.use(function(req, res, next) {

//to allow cross domain requests to send cookie information.
res.header('Access-Control-Allow-Credentials', true);

// origin can not be '*' when crendentials are enabled. so need to set it to the request origin
res.header('Access-Control-Allow-Origin',  req.headers.origin);

// list of methods that are supported by the server
res.header('Access-Control-Allow-Methods','OPTIONS,GET,PUT,POST,DELETE');

res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, X-XSRF-TOKEN');

    next();
});

//机顶盒axios默认是这样的。我为客户端使用React

import axios from 'axios';

const instance = axios.create({
    baseURL: 'http://localhost:5000/',
    withCredentials: true,
});

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form- 
urlencoded';
axios.defaults.withCredentials = true;
axios.defaults.crossDomain = true;

export default instance;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用 axios 在 JavaScript 中发出同步 HTTP GET 请求?

我无法在节点中发出 Web 请求

在ReactJS中发出多个并行axios请求

无法在Android中发出发布请求

无法在 node.js 中发出请求

无法在 Python 中发出 HTTP 请求

如何在Rust中发出GET请求?

在C中发出Get HTTP请求

无法使用 axios 发出 POST 请求

我的问题是我无法在ios 13和xcode 11中发出任何网络请求

Node.js无法通过axios在get请求中发送oauth v1参数

无法从 reactjs 中的 axios 为 id=1 文章向 django REST API 后端发出 GET 请求

我应该如何在axios GET请求中发送JWT令牌?

如何在我的React App上通过Axios从Imgur的API发出GET请求

由于 CORS 问题,无法在 Angular 中发出 HttpClient 发布请求

无法在Dialogflow内联编辑器中发出HTTP请求

无法在.NetCore RestApi中发出发布请求

在Spring Boot中发出Get请求时出错

使用参数在iOS中发出HTTP Get请求

在yii2应用中发出GET请求

如何在扩展的 Django 模板中发出 GET 请求

我可以使用angular4在相同的服务方法中发出两个单独的Http get请求吗?

我无法使用Ruby发出Ajax请求

我如何在 axios 中发出获取请求以通过使用 react.js 单击他的名字来获取用户的信息

Flutter:无法发出HTTP-GET请求

在 React Native 中发出多个 axios 请求以获取不同的数据并输入到屏幕的多个部分

如何使用Express在NodeJS中的GET请求中发出GET请求

如何使用 Python 请求来模拟我在 curl 中发出的 POST 请求?

使用-u参数在节点js中发出等效于CURL请求的GET请求