Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

cors error in react

componentDidMount() {
  fetch('--------------------------------------------',{
    method: "GET",
    headers: {
      "access-control-allow-origin" : "*",
      "Content-type": "application/json; charset=UTF-8"
    }})
  .then(results => results.json())
  .then(info => {
    const results = info.data.map(x => {
      return {
        id: x.id,
        slug: x.slug,
        name: x.name,
        address_1: x.address_1,
        address_2: x.address_2,
        city: x.city,
        state: x.state,
        postal_code: x.postal_code,
        country_code: x.country_code,
        phone_number: x.phone_number,
      }
    })
    this.setState({warehouses: results, lastPage: info.last_page});
  })
  .then(console.log(this.state.warehouses))
 }
Comment

react cors error

package.json
"proxy": "backend localhost i.e., https://localhost:5000"

react frontend
useEffect(() => {
    const res = fetch('/api');
    console.log(res);
  }, [])


backend
app.get('/api', cors(), (req, res) => {
  res.set('Access-Control-Allow-Origin', '*');
  const link = 'https://yourApiLink.com';
  fetch(link).then(response => response.text()).then(text => console.log(text));

});
Comment

react cors error

package.json
"proxy": "backend localhost i.e., https://localhost:5000"

react frontend
useEffect(() => {
    const res = fetch('/api');
    console.log(res);
  }, [])


backend
app.get('/api', cors(), (req, res) => {
  res.set('Access-Control-Allow-Origin', '*');
  const link = 'https://yourApiLink.com';
  fetch(link).then(response => response.text()).then(text => console.log(text));

});
Comment

PREVIOUS NEXT
Code Example
Javascript :: js check if image url exists 
Javascript :: how to set button width in javascript 
Javascript :: javascript trigger keypress 
Javascript :: Node.JS mongodb create database 
Javascript :: json schema validator allows null 
Javascript :: how to use post method axios 
Javascript :: The slice JavaScript string method 
Javascript :: disable zoom in app 
Javascript :: killall node 
Javascript :: javascript string error 
Javascript :: how to prevent xss attacks in node js 
Javascript :: axios set request header 
Javascript :: convert js date to utc 
Javascript :: how to return argument in javascript 
Javascript :: check if element with class has another class javascript 
Javascript :: knex.js migration create 
Javascript :: javascript convert minus to plus 
Javascript :: is javascript faster than python 
Javascript :: add class to element vue 
Javascript :: get data from google sheets javascript 
Javascript :: javascript email validation 
Javascript :: react get route params 
Javascript :: findoneandupdate mongoose 
Javascript :: javascript read word document 
Javascript :: node js split 
Javascript :: how to find missing number in integer array of 1 to 100 in javascript 
Javascript :: jquery vs react 
Javascript :: Using flat() method 
Javascript :: remove duplicates javascript 
Javascript :: AJAX JAVASCRIPT FUNCTION CALLS 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =