在输入字段中快速键入时出现禁止错误
Forbidden Error When typing fast in input field
当我在输入字段中输入太快时,我在浏览器中收到禁止错误 403 。我在后端使用 React Js with Node and Express。
以下是我保存输入的代码。
import React, {Component, PropTypes} from 'react';
export default class SearchBox extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
typing :false,
typingTimeOut :0,
};
this.changeName=this.changeName.bind(this);
this.sendtoParent=this.sendtoParent.bind(this);
}
changeName(event) {
const self=this;
if(self.state.typingTimeOut)
{
clearTimeout(typingTimeOut);
}
self.setState({
name: event.target.value,
typing:false,
typing: setTimeout(function(){
self.sendtoParent(self.state.name)},1000)
});
}
sendtoParent(){
this.props.searching(this.state.name,"true");
}
render() {
return (
<div >
<input
style={styles}
id="SearchBox"
type="text"
placeholder='Enter the name'
onChange={this.changeName}
/>
</div>
);
}
}
我的名字传给了父级,后来父级从 Github 搜索 API. 中给我所需的 json 我的代码运行良好当我正常打字时,它在快速打字时出现 403 错误。
Github 对每秒可以发送的请求数有限制。您正试图在您的方法中引入超时以延迟请求的发送,这是一个很好的方法,但它的实现方式行不通。
最简单的方法是将您的 changeName 函数修改为如下所示:
changeName(event) {
const self=this;
if(self.typingTimeOut)
{
clearTimeout(self.typingTimeOut);
}
self.typingTimeOut = setTimeout(function(){
self.sendtoParent(self.state.name)},1000);
self.setState({
name: event.target.value,
typing:false
});
}
当我在输入字段中输入太快时,我在浏览器中收到禁止错误 403 。我在后端使用 React Js with Node and Express。
以下是我保存输入的代码。
import React, {Component, PropTypes} from 'react';
export default class SearchBox extends Component {
constructor(props) {
super(props);
this.state = {
name: "",
typing :false,
typingTimeOut :0,
};
this.changeName=this.changeName.bind(this);
this.sendtoParent=this.sendtoParent.bind(this);
}
changeName(event) {
const self=this;
if(self.state.typingTimeOut)
{
clearTimeout(typingTimeOut);
}
self.setState({
name: event.target.value,
typing:false,
typing: setTimeout(function(){
self.sendtoParent(self.state.name)},1000)
});
}
sendtoParent(){
this.props.searching(this.state.name,"true");
}
render() {
return (
<div >
<input
style={styles}
id="SearchBox"
type="text"
placeholder='Enter the name'
onChange={this.changeName}
/>
</div>
);
}
}
我的名字传给了父级,后来父级从 Github 搜索 API. 中给我所需的 json 我的代码运行良好当我正常打字时,它在快速打字时出现 403 错误。
Github 对每秒可以发送的请求数有限制。您正试图在您的方法中引入超时以延迟请求的发送,这是一个很好的方法,但它的实现方式行不通。
最简单的方法是将您的 changeName 函数修改为如下所示:
changeName(event) {
const self=this;
if(self.typingTimeOut)
{
clearTimeout(self.typingTimeOut);
}
self.typingTimeOut = setTimeout(function(){
self.sendtoParent(self.state.name)},1000);
self.setState({
name: event.target.value,
typing:false
});
}