The Movie Database 支持

Hello I'm getting this CORS error and can't figure out how to overcome this:

Access to XMLHttpRequest at 'https://api.themoviedb.org/3' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
xhr.js:184 GET https://api.themoviedb.org/3 net::ERR_FAILED

This is my request code:

async function fetchData(){
            const response = await axios.get(requests.selectedOption);
            console.log(response);
            setMovies(response.data.results);
            return response;
        }

and this is config 1:

import axios from 'axios';

const instance = axios.create({
    baseURL: 'https://api.themoviedb.org/3',
});

export default instance;

and this is config 2:

const API_KEY = here goes my api key;

export default {
    fetchTrending: `/trending/all/week?api_key=${API_KEY}&language=en-US`,
    fetchTopRated: `/movie/top_rated?api_key=${API_KEY}&language=en-US`,
    fetchActionMovies: `/discover/movie?api_key=${API_KEY}&with_genres=28`,
    fetchComedyMovies: `/discover/movie?api_key=${API_KEY}&with_genres=35`,
    fetchHorrorMovies: `/discover/movie?api_key=${API_KEY}&with_genres=27`,
    fetchRomanceMovies: `/discover/movie?api_key=${API_KEY}&with_genres=10749`,
    fetchMystery: `/discover/movie?api_key=${API_KEY}&with_genres=9648`,
    fetchSciFi: `/discover/movie?api_key=${API_KEY}&with_genres=878`,
    fetchWestern: `/discover/movie?api_key=${API_KEY}&with_genres=37`,
    fetchAnimation: `/discover/movie?api_key=${API_KEY}&with_genres=16`,
    fetchTV: `/discover/movie?api_key=${API_KEY}&with_genres=10770`,
}

32 回复(第 1 页,共 3 页)

Jump to last post

下一页末页

First off, I am not familiar with Axios, so I can only assume a few things based on what you posted here. It looks like Axios is treating your base URL as a resolvable URL for some type of a preflight request. But if you go to https://api.themoviedb.org/3, you'll see that it's not a resolvable URL (nothing exists there).

Soooo... as a temporary thing to try, don't use baseURL. An OPTIONS call to any of the actual methods, returns the correct headers, example:

Request

$ curl -i -X OPTIONS "https://api.themoviedb.org/3/trending/all/week?api_key=${API_KEY}&language=en-US"

Response

HTTP/2 204
date: Mon, 07 Sep 2020 15:36:22 GMT
server: openresty
access-control-allow-origin: *
access-control-allow-methods: GET, HEAD, POST, PUT, DELETE, OPTIONS
access-control-expose-headers: ETag, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, Content-Length, Content-Range
access-control-allow-headers: Authorization, Content-Type, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Range
access-control-max-age: 1728000
x-cache: Miss from cloudfront
via: 1.1 ceb7b8c925a9435b9b08b23014561fbb.cloudfront.net (CloudFront)
x-amz-cf-pop: HIO51-C1
x-amz-cf-id: DRD3f1RYVwx5l3Pgdx4whaQhlhAoAYcCFsfF28wUDDHTORDnF5f1Sw==

Creating a new resolvable URL at https://api.themoviedb.org/3 is certainly doable, but it does not exist right now.

@travisbell said:

First off, I am not familiar with Axios, so I can only assume a few things based on what you posted here. It looks like Axios is treating your base URL as a resolvable URL for some type of a preflight request. But if you go to https://api.themoviedb.org/3, you'll see that it's not a resolvable URL (nothing exists there).

Soooo... as a temporary thing to try, don't use baseURL. An OPTIONS call to any of the actual methods, returns the correct headers, example:

Request

$ curl -i -X OPTIONS "https://api.themoviedb.org/3/trending/all/week?api_key=${API_KEY}&language=en-US"

Response

HTTP/2 204
date: Mon, 07 Sep 2020 15:36:22 GMT
server: openresty
access-control-allow-origin: *
access-control-allow-methods: GET, HEAD, POST, PUT, DELETE, OPTIONS
access-control-expose-headers: ETag, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, Content-Length, Content-Range
access-control-allow-headers: Authorization, Content-Type, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, Retry-After, DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Range
access-control-max-age: 1728000
x-cache: Miss from cloudfront
via: 1.1 ceb7b8c925a9435b9b08b23014561fbb.cloudfront.net (CloudFront)
x-amz-cf-pop: HIO51-C1
x-amz-cf-id: DRD3f1RYVwx5l3Pgdx4whaQhlhAoAYcCFsfF28wUDDHTORDnF5f1Sw==

Creating a new resolvable URL at https://api.themoviedb.org/3 is certainly doable, but it does not exist right now.

Hi there,

I'm facing with the same issue. If I make a request browser return me CORS problem. I've tried to used heroku cors everywhere but without success.

There is another workaround or you can solve this step?

thanks

@kreo said:

Hello I'm getting this CORS error and can't figure out how to overcome this:

Access to XMLHttpRequest at 'https://api.themoviedb.org/3' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
xhr.js:184 GET https://api.themoviedb.org/3 net::ERR_FAILED

This is my request code:

async function fetchData(){
            const response = await axios.get(requests.selectedOption);
            console.log(response);
            setMovies(response.data.results);
            return response;
        }

and this is config 1:

import axios from 'axios';

const instance = axios.create({
    baseURL: 'https://api.themoviedb.org/3',
});

export default instance;

and this is config 2:

const API_KEY = here goes my api key;

export default {
    fetchTrending: `/trending/all/week?api_key=${API_KEY}&language=en-US`,
    fetchTopRated: `/movie/top_rated?api_key=${API_KEY}&language=en-US`,
    fetchActionMovies: `/discover/movie?api_key=${API_KEY}&with_genres=28`,
    fetchComedyMovies: `/discover/movie?api_key=${API_KEY}&with_genres=35`,
    fetchHorrorMovies: `/discover/movie?api_key=${API_KEY}&with_genres=27`,
    fetchRomanceMovies: `/discover/movie?api_key=${API_KEY}&with_genres=10749`,
    fetchMystery: `/discover/movie?api_key=${API_KEY}&with_genres=9648`,
    fetchSciFi: `/discover/movie?api_key=${API_KEY}&with_genres=878`,
    fetchWestern: `/discover/movie?api_key=${API_KEY}&with_genres=37`,
    fetchAnimation: `/discover/movie?api_key=${API_KEY}&with_genres=16`,
    fetchTV: `/discover/movie?api_key=${API_KEY}&with_genres=10770`,
}

I'm facing similar issue, and looking at the code, looks like you too are following the Clever Programmer tutorial for Netflix clone. Please let me know what worked for you.

@kausarshaikh said:

@kreo said:

I'm having same Issue. May i know the issue and the solution?

Im following clever programmer and im having the same issue

I'm facing the same issue as well.

If the explanation given by Travis Bell did not solve or help, then perhaps one of these conversations can help. Perhaps.

CORS issue

In case you have not researched here in the Forum, below the link of 2 posts with similar problem.
Maybe it helps:
https://www.themoviedb.org/talk/5f621eae93388b003864513c
https://www.themoviedb.org/talk/5eb07271a13533001b72740a

axios is properly working for me, using it client side. Here's an example of my code

"axios": "^0.21.0",
const options = {
  method: 'GET',
  url: 'https://api.themoviedb.org/3/movie/603',
  params: {
    api_key: 'api-key'
  }
}

axios(options)
  .then(data => console.log(data))
  .catch(error => console.log(error))

replace your axios.js file with this

import axios from "axios";

const config = {
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "text/plain",
  },
};
const instance = axios.create({
  baseURL: "https://api.themoviedb.org/3",
  https: config,
});

export default instance;

@rafidah said:

@kausarshaikh said:

@kreo said:

I'm having same Issue. May i know the issue and the solution?

Hello did anyone find a solution I am getting the same error too

@zaferozcan said:

replace your axios.js file with this

import axios from "axios";

const config = {
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "text/plain",
  },
};
const instance = axios.create({
  baseURL: "https://api.themoviedb.org/3",
  https: config,
});

export default instance;

I tried this but still I am getting the same error

you find any solution yet ?

Hey guys I'm also facing the same issue and the clover programmmmmmer didn't say properly that they may have missed out something which we don't know.

import axios from "axios";

const config = { headers: { "Access-Control-Allow-Origin": "*", "Content-Type": "text/plain", }, }; const instance = axios.create({ baseURL: "https://api.themoviedb.org/3", https: config, });

export default instance;

Really it works so well i got it ..

Before going to execute the code you should have to disable the CORS issue by running the commends

@PrawinPravs said:

import axios from "axios";

const config = { headers: { "Access-Control-Allow-Origin": "*", "Content-Type": "text/plain", }, }; const instance = axios.create({ baseURL: "https://api.themoviedb.org/3", https: config, });

export default instance;

Really it works so well i got it ..

Before going to execute the code you should have to disable the CORS issue by running the commends

how this working with you i am doing same this code and not working for me

找不到电影或剧集?登录并创建它吧。

全站通用

s 聚焦到搜索栏
p 打开个人资料菜单
esc 关闭打开的窗口
? 打开键盘快捷键窗口

在媒体页面

b 返回(或返回上级)
e 进入编辑页面

在电视季页面

(右箭头)下一季
(左箭头)前一季

在电视集页面

(右箭头)下一集
(左箭头)前一集

在所有图像页面

a 打开添加图片窗口

在所有编辑页面

t 打开翻译选择器
ctrl+ s 提交

在讨论页面

n 创建新讨论
w 切换关注状态
p 设为公开 / 私密讨论
c 关闭 / 开放讨论
a 打开活动页
r 回复讨论
l 跳转至最新回复
ctrl+ enter 发送信息
(右箭头)下一页
(左箭头)前一页

设置

想给这个条目评分或将其添加到片单中?

登录

还不是会员?

注册加入社区