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

javascript - Access-Control-Allow-Origin axios request error - CORS - Stack Overflow

programmeradmin3浏览0评论

I have a problem with CORS while sending POST request:

Here is my code:

import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:8084',
})


export const postTip = async (payload) => {
  try {
    const { data } = await api.post(`post-tip`, payload);
    return data;
  } catch (e) {
    return [];
  }
};

I have found that I need to set Access-Control-Allow-Origin in headers, and I did it in 2 ways. First one:

const api = axios.create({
  baseURL: 'http://localhost:8084',
  headers: {
    post: {
      "Access-Control-Allow-Origin": true
    }
  }
})

And the second one:

const { data } = await api.post(`post-tip`, payload, {
      headers: {
        "Access-Control-Allow-Origin": true
      }
    });

Both of them work and in request header I can see this line Access-Control-Allow-Origin: true. But I still get this error, so, what's the problem?

I have a problem with CORS while sending POST request:

Here is my code:

import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:8084',
})


export const postTip = async (payload) => {
  try {
    const { data } = await api.post(`post-tip`, payload);
    return data;
  } catch (e) {
    return [];
  }
};

I have found that I need to set Access-Control-Allow-Origin in headers, and I did it in 2 ways. First one:

const api = axios.create({
  baseURL: 'http://localhost:8084',
  headers: {
    post: {
      "Access-Control-Allow-Origin": true
    }
  }
})

And the second one:

const { data } = await api.post(`post-tip`, payload, {
      headers: {
        "Access-Control-Allow-Origin": true
      }
    });

Both of them work and in request header I can see this line Access-Control-Allow-Origin: true. But I still get this error, so, what's the problem?

Share Improve this question asked Oct 10, 2021 at 18:43 dokichandokichan 6414 gold badges22 silver badges58 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

This error from your backend, you use axios post from front-end, but you need to set header "Access-Control-Allow-Origin": true on your backend.

this error is probably ing from the server. Are you trying to reach your own api? If you do the error might be there.

I see you tagged express so please refer to this page it should contain all the information you need

http://expressjs./en/resources/middleware/cors.html

发布评论

评论列表(0)

  1. 暂无评论