Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在之间启用CORShttp://localhost:3000/ 以及ReactJS中的TMDB api?_Reactjs_Api_Axios_Cors - Fatal编程技术网

如何在之间启用CORShttp://localhost:3000/ 以及ReactJS中的TMDB api?

如何在之间启用CORShttp://localhost:3000/ 以及ReactJS中的TMDB api?,reactjs,api,axios,cors,Reactjs,Api,Axios,Cors,我面临的错误是无法从TMDB API获取数据 访问位于“”的XMLHttpRequesthttps://api.themoviedb.org/3“起源”http://localhost:3000'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头 axios.js安装程序 import axios from "axios"; const instance = axios.create({ baseURL: &qu

我面临的错误是无法从TMDB API获取数据

访问位于“”的XMLHttpRequesthttps://api.themoviedb.org/3“起源”http://localhost:3000'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头

axios.js安装程序

import axios from "axios";  
const instance = axios.create({
    baseURL: "https://api.themoviedb.org/3",
})  
export default instance;
request.js安装程序:

const API_KEY = "xxx";  
const request = {  
  fetchTrending: `/trending/all/week?api_key=${API_KEY}&language=en-US`,  
  fetchNetflixOriginal: `/discover/tv?api_key=${API_KEY}&with_networks=213`,  
}  
export default request;
App.js

useEffect(() => {
        //if [], run once when row load, and dont run again
        console.log("rrrrsd >>> ", typeof(fetchUrl))

        async function fetchData(){
            const response = await axios.get(fetchUrl, {
                headers:{
                    'Access-Control-Allow-Origin': '*',
                }
            }).then(function (response){
                console.log('response >>>>', response.data)
            }).catch(function (error){
                if(error.response){
                    console.log('response error >>>', error.response.headers)
                }
                else if(error.request){
                    console.log('error request >>>', error.request)
                }
                else{
                    console.log('message error >>>', error.message)
                }
            })

        }
        fetchData()
    },[fetchUrl])

请帮助我在TMDB API和ReactJS localHost之间启用CORS

使用google chrome扩展,如(CORS域)或(允许CORS)
昨天我遇到了同样的问题,扩展修复了它

对于我来说,它仍然在“从源”错误处提供对XMLHttpRequest的访问。出于测试目的,我将fetchUrl更改为“”,它将提供数据。我怀疑fetchUrl不正确。@rafalias您尝试过扩展吗?是的,我在localHost:3000上打开了允许Cors