0

Cuando quiero eliminar un dato desde la aplicación a una base de datos en Firebase:

Access to fetch at 'https://fir-movie-app-2761f-default-rtdb.firebaseio.com/movies.json/-Mlv85nQwY-xehVg_Tn2' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Movie.js:8 DELETE https://fir-movie-app-2761f-default-rtdb.firebaseio.com/movies.json/-Mlv85nQwY-xehVg_Tn2 net::ERR_FAILED

Mi código es :

import React from 'react';

import classes from './Movie.module.css';

const Movie = props => {
  const deleteHandler = async () => {
    try {
      const response = await fetch(
        `https://fir-movie-app-2761f-default-rtdb.firebaseio.com/movies.json/${props.id}`,
        {
          method: 'DELETE',
          headers: {
            'Content-type': 'application/json',
          },
        }
      );

      if (!response.ok) {
        throw new Error('Something went wrong ' + response.status);
      }

      const data = await response.json();
      console.log(data);
    } catch (error) {
      alert(error);
    }
  };

  return (
    <li className={classes.movie}>
      <h2>{props.title}</h2>
      <h3>{props.releaseDate}</h3>
      <p>{props.openingText}</p>
      <button onClick={deleteHandler}>X</button>
    </li>
  );
};

export default Movie;

1 respuesta 1

1

es un error de cors, si no te funciona de esta manera, tendras que habilitarlo desde tu backend

const response = await fetch(
        `https://fir-movie-app-2761f-default-rtdb.firebaseio.com/movies.json/${props.id}`,
        {
          method: 'DELETE',
          headers: {
            'Content-type': 'application/json',
             'Access-Control-Allow-Origin': '*',
             'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
          },
        }
      );

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.