我想通过此表单将数据发布到我的API。但不幸的是,这是服务器错误。有人能说出问题到底在哪里吗? (我观察到没有为该对象生成ID)单击提交按钮后,这是我进入控制台的错误:
POSThttps://alert-amigo-api.herokuapp.com/products500(内部服务器错误)响应{类型:“ cors”,网址:“ https://alert-amigo-api.herokuapp.com/products”,重定向:false,状态:500,确定:否,…}类型:“ cors”网址:重定向“ https://alert-amigo-api.herokuapp.com/products”:否状态:500正常:错误statusText:“内部服务器错误”
这是我的代码:
import React, { Component } from "react";
import { Link } from 'react-router-dom';
import { Form, FormControl, FormCheck } from 'react-bootstrap';
import { FormGroup, ControlLabel, Row, Button, Checkbox, Radio, HelpBlock } from "react-bootstrap";
function FieldGroup({ id, label, help, ...props }) {
return (
<FormGroup controlId={id}>
<ControlLabel>{label}</ControlLabel>
<FormControl {...props} />
{help && <HelpBlock>{help}</HelpBlock>}
</FormGroup>
);
}
class Typography extends Component {
constructor() {
super();
this.state = {
productName: '',
productPrice: '',
productCategory: '',
productBrand: '',
countryOfOrigin: '',
riskType: '',
alertSubmittedBy: '',
yourCity: '',
yourAddress: '',
productImage: '',
description: ''
};
this.handleSubmit = this.handleSubmit.bind(this);
}
changeHandler = e => {
this.setState({[e.target.id]: e.target.value})
}
handleSubmit(e) {
e.preventDefault();
console.log('The form was submitted with the following data:');
console.log(this.state);
fetch('https://alert-amigo-api.herokuapp.com/products',{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(this.state)
}).then(response => {
console.log(response)
})
.catch(error =>{
console.log(error)
});
}
render() {
return (
<div className="typoForm">
<form onSubmit={this.handleSubmit}>
<FieldGroup
id="productName"
name="productName"
type="text"
label="Product Name"
placeholder=""
value={this.state.productName}
onChange={this.changeHandler}
/>
<FieldGroup
id="productPrice"
name="productPrice"
type="number"
label="Product Price (in Euros)"
placeholder=""
value={this.state.productPrice}
onChange={this.changeHandler}
/>
<FormGroup controlId="productCategory" name="productCategory">
<ControlLabel>Category</ControlLabel>
<FormControl componentClass="select" name="productCategory" placeholder="select" onChange={this.changeHandler} value={this.state.selectValue}>
<option value="select">select the category to which the product belongs to</option>
<option value="electronics">Electronics</option>
<option value="cosmetics">Cosmetics</option>
<option value="apparels">Apparels</option>
<option value="footwear">Footwear</option>
<option value="accessories">Watches/Accessories</option>
<option value="handbags">Handbags/Wallets</option>
<option value="pharmaceuticals">Pharmaceuticals/Personal Care</option>
<option value="Toys">Toys</option>
</FormControl>
</FormGroup>
<FieldGroup
id="productBrand"
name="productBrand"
type="text"
label="Product Brand"
placeholder=""
value={this.state.productBrand}
onChange={this.changeHandler}
/>
<FieldGroup
id="countryOfOrigin"
name="countryOfOrigin"
type="text"
label="Country Of Origin"
placeholder=""
value={this.state.countryOfOrigin}
onChange={this.changeHandler}
/>
<FormGroup controlId="riskType" name="riskType">
<ControlLabel>Risk Type</ControlLabel>
<FormControl componentClass="select" placeholder="select" name="riskType" onChange={this.changeHandler} value={this.state.selectValue}>
<option value="select">select the level of risk</option>
<option value="high">high</option>
<option value="medium">medium</option>
<option value="low">low</option>
</FormControl>
</FormGroup>
<FormGroup controlId="alertSubmittedBy" name="alertSubmittedBy">
<ControlLabel>Alert Submitted By</ControlLabel>
<FormControl componentClass="select" onChange={this.changeHandler} name="alertSubmittedBy" placeholder="select" value={this.state.selectValue}>
<option value="select">select</option>
<option value="producers">producers</option>
<option value="consumers">consumers</option>
<option value="distributors">distributors</option>
</FormControl>
</FormGroup>
<FieldGroup
id="yourCity"
name="yourCity"
type="text"
label="Your City"
placeholder=""
value={this.state.yourCity}
onChange={this.changeHandler}
/>
<FormGroup controlId="yourAddress" name="yourAddress">
<ControlLabel>Your Address</ControlLabel>
<FormControl componentClass="textarea" name="yourAddress" onChange={this.changeHandler} value={this.state.value} placeholder="Enter your address here" />
</FormGroup>
<FieldGroup
id="productImage"
name="productImage"
type="file"
label="File"
value={this.state.value}
onChange={this.changeHandler}
help="Upload an image of the product."
/>
<FormGroup controlId="description" name="description">
<ControlLabel>Please mention the defaults of the product</ControlLabel>
<FormControl componentClass="textarea" name="description" onChange={this.changeHandler} value={this.state.value} placeholder="" />
</FormGroup>
<Button type="submit">Submit</Button>
</form>
</div>
);
}
}
export default Typography;
500错误代码是服务器端错误。因此,您应该调试API代码,而不是对代码进行调试,以深入研究问题。
话虽如此,错误消息表示CORS错误。如果您不熟悉Cross-Origin Resource Sharing (CORS),建议您先阅读并熟悉该内容,然后再继续进行故障排除。
此错误很可能是由于托管您的reactjs组件的域与托管您的api的域不同(alert-amigo-api.herokuapp.com,并且1)该API不相同的事实引起的为跨域请求配置,或2)为CORS配置,但是您的reactjs客户端尚未设置正确的标头来启用CORS所需的飞行前请求。
请查看以下内容以获取更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORShttps://cors-anywhere.herokuapp.com/https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/cors.md