I have a select form from MaterialUI that is populated with choices that I received via an axios GET
request. This works, however, I now need to axios POST
request the selected option in my handleChange(event)
function. The data I need to POST
from the select drop down should be the string
value of the MenuItem
, not an index or key.
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
flexWrap: 'wrap',
},
formControl: {
margin: theme.spacing(1),
minWidth: 250,
},
selectEmpty: {
marginTop: theme.spacing(1),
},
}));
function SimpleSelect() {
const classes = useStyles();
const [values, setValues] = React.useState({
firm: '',
infosys: '',
spot: '',
});
const [choices, setChoices] = React.useState([])
const inputLabel = React.useRef(null);
const [labelWidth, setLabelWidth] = React.useState(100)
useEffect(() => {
const fetchData = async () => {
const result = await axios.get(
'http://127.0.0.1:5000/form/',
);
console.log('RESULT DATA: ', result.data)
setChoices(result.data)
};
fetchData();
}, []);
useEffect(() => {
console.log(values);
}, [values]);
Below is the area where the issue likely is:
function handleChange(event) {
setValues({
firm: event.target.value
})
}
const selectOptions = choices.map((choice, index) =>
<MenuItem key={index} value={choice} primarytext={choice}>{choice}
</MenuItem>
)
return (
<form className={classes.root} autoComplete="off">
<FormControl className={classes.formControl}>
<InputLabel htmlFor="firm-helper">Firm</InputLabel>
<Select
value={values.firm}
onChange={handleChange}
input={<Input name="firm" id="firm-helper" />}
>
{selectOptions}
</Select>
<FormHelperText>Select a Firm</FormHelperText>
</FormControl>
<form>
)
I expect to be able to axios POST
request in my handleChange
function whenever the selected option in the drop down menu changes. I wish to POST
the firm
value in my values
dictionary
Current Output:
Access to XMLHttpRequest at 'http://127.0.0.1:5000/form/' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
xhr.js:166 POST http://127.0.0.1:5000/form/ net::ERR_FAILED