Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

reactjs - How to change the access control allow origin to url api in react

enter image description here

How to change the access control allow origin to url api, cause every time I load the page I'm getting an error which is the error 500.

When I login I added this code

 const options = {
          headers: {
            'Content-Type': 'application/json',
          }
        }

Axios.post("/auth", payload, options).then(function (response: AxiosResponse) { ....}

then I create a service for calling api.

api

function useApi() {

    const creds = useCredentials();

    Axios.defaults.baseURL = serverConfig[creds.server].api;

    return {
        get: (id: string) => Axios.get(`${API_URL}/${id}`),
        getAll: (params?: object) => Axios.get(API_URL + (params ? getQueryParams(params) : '')),
        getLatest: () => Axios.get(`${API_URL}/latest`)
    };
}

export default useApi;

But when I call the getLatest on my index.tsx which is like this

function Home() {

const [state, setState] = useState([]);
const { getLatest } = useApi();
const PARAMS = { length: '12' }

useEffect(() => {
getAll(PARAMS).then(async (response) => setRowData(response['data'].data));
}, []);

return (<></>);
}

then the error is the photo above.

question from:https://stackoverflow.com/questions/65838595/how-to-change-the-access-control-allow-origin-to-url-api-in-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I can see in your picture, your API is response 500, this status code throw when your server is stuck and error, not about CORS.

A little bit about CORS:

  • Not relevant for React.js. Let's check and disable CORS on your server.

  • Or you can consider disabling CORS temporary for development purpose by adding prefix: https://cors-anywhere.herokuapp.com/${YOUR_URL_API}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...