CORS问题-在React代码中的哪里实现解决方案?

时间:2019-01-12 23:07:46

标签: javascript reactjs ecmascript-6 cors axios

我在哪里放置{Access-Control-Allow-Origin:*}?

我了解CORS的问题,似乎{Access-Control-Allow-Origin:*}是解决方案,但我不知道将它放在React代码中的什么位置。我不想使用该Chrome扩展程序。我不能要求网站访问者修改他们的浏览器。

我查看了以下帖子:Access Control Origin Header error using Axios in React Web throwing error in Chromehttps://developer.mozilla.org/en-US/docs/Web/HTTP/CORShttps://medium.com/@baphemot/understanding-cors-18ad6b478e2bhttps://daveceddia.com/access-control-allow-origin-cors-errors-in-react-express/

我正在尝试使用Axios访问iTunes Search API。它在开发中运行良好,但是一旦存在于GitHub Pages上,就会出现一些搜索问题。我使用create-react-app制作了该应用程序。据我所知,没有专用的服务器文件。

GitHub:https://github.com/irene-rojas/itunes-react

应用

import React, { Component } from 'react';
import './App.css';
import axios from "axios";

class App extends Component {

state = {
    term: "",
    results: [],
    gridSpace: 0
};

// text field
onChange = (event) => {
    this.setState({
        term: event.target.value,
    });
    // console.log(this.state.term);
}

onSubmit = (event) => {
    event.preventDefault();
    axios.get(`https://itunes.apple.com/search?term=${this.state.term.replace(/ /g,"+")}&entity=musicTrack&limit=20`)
    .then(res => {
        this.setState({ 
            results: res.data.results,
            gridSpace: this.state.gridSpace + 1
         });
        //  console.log(this.state.results);
    });
}

  render() {
    return (

<div className="parallax">

  <div className="App">

    <div className="header">

        <div className="title">
            <h1>Search iTunes!</h1>

            <form onSubmit={this.onSubmit} className="searchBar">
                Search by artist: <input value={this.state.term} onChange={this.onChange} /> 
                <button>Search!</button>
            </form>
            <br></br>
            Top 20 Results

        </div>

    </div> 

    <div className="masterDiv">

        <div className="resultsDiv">
            {this.state.results.map((result, index) => 
            <div className={`card grid${index} zoom`} key={result.trackId}>
            <a href={result.trackViewUrl} target="_blank" rel="noopener noreferrer" key={index}>
                <div>
                    <img src={result.artworkUrl100} alt="album art"/>
                        <br></br>
                    Song: {result.trackName}
                        <br></br>
                    Artist: {result.artistName}
                        <br></br>
                    Album: {result.collectionName}
                </div>
                </a>
            </div>
            )}

        </div>
        {/* end resultsDiv */}

    </div>  
    {/* end masterDiv */}

    </div>
    {/* end App */}

</div>
// end parallax

    );
  }
}

export default App;

package.json

{
  "name": "itunes-react",
  "version": "0.1.0",
  "private": true,
  "homepage": "http://irene-rojas.github.io/itunes-react",
  "dependencies": {
    "axios": "^0.18.0",
    "gh-pages": "^2.0.1",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-scripts": "2.1.2"
  },
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

0 个答案:

没有答案