Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

spring rest api cors error in react app

add below line of code in fetch

  export function getData(endpoint = '/', request = {}) {
    let url = endpoint;
    let req = Object.assign(
      {
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*',
        },
      },
      request
    );
    console.log('url,req', url, req);
    return fetch(url, req).then(handleResponse).catch(handleErrors);
  }
And Add below line below Rest Controller class

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
class TestController{}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #spring #rest #api #cors #error #react #app
ADD COMMENT
Topic
Name
5+9 =