How to allow CORS in react.js?

后端 未结 8 1598
[愿得一人]
[愿得一人] 2020-12-25 12:11

I\'m using Reactjs and using API through AJAX in javascript. How can we resolve this issue? Previously I used CORS tools, but now I need to enable CORS.

8条回答
  •  -上瘾入骨i
    2020-12-25 12:38

    Suppose you want to hit https://yourwebsitedomain/app/getNames from http://localhost:3000 then just make the following changes:

    packagae.json :

       "name": "version-compare-app",
       "proxy": "https://yourwebsitedomain/",
       ....
       "dependencies": {
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.5.0",
         ...
    

    In your component use it as follows:

    import axios from "axios";
    
    componentDidMount() {
    const getNameUrl =
      "app/getNames";
    
    axios.get(getChallenge).then(data => {
      console.log(data);
    });
    

    }

    Stop your local server and re run npm start. You should be able to see the data in browser's console logged

提交回复
热议问题