[英]React and axios - sending GET and POST requests instead of only POST
In React I have this form being submitted:在 React 中,我提交了这个表单:
<form onSubmit={ (event) => this.handleSubmitOrder(event) }>
<div className="field">
<input
name="client"
className="input is-dark is-large"
type="text"
placeholder="Client name"
required
//value={this.state.formClient.client}
onChange={this.handleOrderFormChange}
/>
</div>
<div className="field">
<input
name="email"
className="input is-dark is-large"
type="text"
placeholder="Client email"
required
//value={this.state.formClient.email}
onChange={this.handleOrderFormChange}
/>
</div>
<input
type="submit"
className="button is-dark is-large is-fullwidth"
value="Submit"
/>
</form>
This is handleSubmitOrder()
, handling a POST
request:这是handleSubmitOrder()
,处理一个POST
请求:
handleSubmitOrder(event) {
const {userId} = this.props
const data = {
client: this.state.formClient.client,
email: this.state.formClient.email,
};
var headers = {
'Content-Type': 'application/json',
//'Access-Control-Allow-Origin': true,
Authorization: `Bearer ${window.localStorage.authToken}`
}
const url = `${process.env.REACT_APP_WEB_SERVICE_URL}/orders/${userId}`;
axios.post(url, data, {headers: headers})
.then((res) => {
console.log(data);
})
.catch((err) => {
});
};
I use nginx
as proxy server (nginx.conf):我使用nginx
作为代理服务器(nginx.conf):
location /orders {
proxy_pass http://web:5000;
proxy_redirect default;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
and at my Flask server, I have my view, specifically declaring POST
method only:在我的 Flask 服务器上,我有我的看法,特别是仅声明POST
方法:
@orders_bp.route('/orders/<user_id>', methods=['POST'])
def orders(user_id):
# business logic
return jsonify(response_object), 200
This endpoint is being reached alright, and logic at /orders
is being successfully processed.此端点已正常到达,并且/orders
处的逻辑已成功处理。
But when I submit the form, I'm getting a 404
error at browser, and, to my confusion, a GET request:但是当我提交表单时,浏览器出现404
错误,令我困惑的是,还有一个 GET 请求:
"GET /orders?client=Ozorio&email=ozo%40gmail.com&phone=118888888&select=pick HTTP/1.0" 404 -
In case it helps, I also have url declared here:如果有帮助,我还在这里声明了 url:
App.jsx:应用程序.jsx:
<Route exact path='/orders' render={() => (
<Orders
formType={'Orders'}
isAuthenticated={this.state.isAuthenticated}
/>
)} />
and here at navbar, where I ask for the url:在导航栏,我要求 url:
{props.isAuthenticated &&
<Link to="/orders" className="navbar-item">Orders</Link>
}
note: I am authenticated.注意:我已通过身份验证。
What could be wrong here?这里有什么问题?
The way forms behave by default is rather interesting. forms 默认的行为方式相当有趣。 You can read more about it here你可以在这里阅读更多关于它的信息
At the end of the day you need to add在一天结束时,您需要添加
event.preventDefault()
at the top of you handleSubmitOrder
function (to prevent the default behavior of submitting a form)在你的顶部handleSubmitOrder
function(防止提交表单的默认行为)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.