Question: Getting the new error message now. Please help. Thank you. What changes would be needed to compile and render successfully? gear: 1 Access to fetch

Getting the new error message now. Please help. Thank you.
What changes would be needed to compile and render successfully?
gear:1Access to fetch at 'My URL' from origin 'localhost:3000'has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
My URL/api/gear:1
Failed to load resource: net::ERR_FAILED
My URL/api/gear:1
Failed to load resource: the server responded with a status of 404()
Here is the DataForm code:
import React, {useState }from 'react';
const DataForm =({addNewItem, updateItem, deleteItem })=>{
const [name,setName]=useState('');
const [brand,setBrand]=useState('');
const [price,setPrice]=useState('');
const [image,setImage]=useState(null);
const [error,setError]=useState(null);
const [success,setSuccess]=useState(false);
const handleImageChange =(e)=>{
setImage(e.target.files[0]);
};
const handleSubmit =async (e)=>{
e.preventDefault();
if (!name.trim()||!brand.trim()||!price.trim()||!image){
setError('All fields are required, including an image.');
setSuccess(false);
return;
}
const formData =new FormData();
formData.append('name',name);
formData.append('brand',brand);
formData.append('price',price);
formData.append('image',image);
try {
const response =await fetch('My URL/api/gear',{
method: 'POST',
body: formData,
});
const data =await response.json();
if (data.success){
setSuccess(true);
addNewItem(data.newItem);
setName('');
setBrand('');
setPrice('');
setImage(null);
}else {
setError(data.message ||'Failed to add the item.');
setSuccess(false);
}
}catch (err){
setError('An error occurred while submitting the form.');
setSuccess(false);
}
};
return (
Name
setName(e.target.value)}/>
Brand
setBrand(e.target.value)}/>
Price
setPrice(e.target.value)}/>
Image
{error && {error}}
{success && Item added successfully!}
Submit
);
};
export default DataForm;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!