无法从 React 表单 post 数据到我的 API
Unable to post data to my API from a react form
我想从这个表格 post 数据到我的 API。但不幸的是,出现了服务器错误。谁能说出到底是什么问题? (我观察到没有为对象生成 id)
这是我在单击提交按钮后在控制台中遇到的错误:
POST https://alert-amigo-api.herokuapp.com/products 500 (Internal
Server Error) Response {type: "cors", url:
"https://alert-amigo-api.herokuapp.com/products", redirected: false,
status: 500, ok: false, …} type: "cors" url:
"https://alert-amigo-api.herokuapp.com/products" redirected: false
status: 500 ok: false statusText: "Internal Server Error"
这是我的代码:
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 错误代码是 server-side 错误。因此你应该调试你的 API 代码,而不是你的反应代码来找到问题的根源。
话虽如此,错误消息表明存在 CORS 错误。如果您不熟悉 Cross-Origin Resource Sharing (CORS),我鼓励您在继续进行故障排除之前阅读并熟悉它。
此错误很可能是由于托管您的 reactjs 组件的域与托管您的 api 的域不同 (alert-amigo-api.herokuapp.com) ,或者 1) API 没有为 cross-origin 请求配置,或者 2) 它被配置为 CORS,但是你的 reactjs 客户端没有设置正确的 headers 来启用 pre-flight CORS 所需的请求。
查看以下内容了解更多信息:
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
https://cors-anywhere.herokuapp.com/
https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9
https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/cors.md
我想从这个表格 post 数据到我的 API。但不幸的是,出现了服务器错误。谁能说出到底是什么问题? (我观察到没有为对象生成 id) 这是我在单击提交按钮后在控制台中遇到的错误:
POST https://alert-amigo-api.herokuapp.com/products 500 (Internal Server Error) Response {type: "cors", url: "https://alert-amigo-api.herokuapp.com/products", redirected: false, status: 500, ok: false, …} type: "cors" url: "https://alert-amigo-api.herokuapp.com/products" redirected: false status: 500 ok: false statusText: "Internal Server Error"
这是我的代码:
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 错误代码是 server-side 错误。因此你应该调试你的 API 代码,而不是你的反应代码来找到问题的根源。
话虽如此,错误消息表明存在 CORS 错误。如果您不熟悉 Cross-Origin Resource Sharing (CORS),我鼓励您在继续进行故障排除之前阅读并熟悉它。
此错误很可能是由于托管您的 reactjs 组件的域与托管您的 api 的域不同 (alert-amigo-api.herokuapp.com) ,或者 1) API 没有为 cross-origin 请求配置,或者 2) 它被配置为 CORS,但是你的 reactjs 客户端没有设置正确的 headers 来启用 pre-flight CORS 所需的请求。
查看以下内容了解更多信息:
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
https://cors-anywhere.herokuapp.com/
https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9
https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/cors.md