如果我的 api 将查询视为 http://localhost:8000/api/v1/rental/?place__startswith=kathmandu那么我如何在reactjs中进行通用搜索。我尝试的是传递默认参数作为搜索(query=kathmandu),以便默认列出名为 kathmandu 的地点的结果,当用户输入他们想要搜索的地点名称时,它应该显示这些地点而不是 kathmndu。但我收到一条错误消息 Uncaught ReferenceError: kathmandu is not Defined。我该如何解决这个问题?
componentWillMount(){
this.search();
}
search(query=kathmandu){
let url = 'http://localhost:8000/api/v1/rental/?place__startswith=query';
Request.get(url).then((response) => {
console.log('response',response.body.objects);
this.setState({
place:response.body.objects
});
});
}
searchUpdated(term){
console.log('term is',term);
this.search(term);
}
render() {
var margin = { marginTop : '13em' };
let location = _.map(this.state.place, (place) => {
return(
<div className="searchResult">
<li>{place.place}</li>
<li>{place.city}</li>
</div>
)
});
return(
<div className = "container">
<div className="content text-align-center">
<div className="row text-xs-center">
<div className="middle-text" style={margin}>
<h1 className="welcome"><span>Welcome </span></h1>
<button ref="test" className="btn how-it-works" onClick={this.handleClick}>Search Space</button>
</div>
</div>
</div>
<div id="mySearch" className="overlay" onKeyDown={this.handleKeyDown}>
<button className="btn closebtn" onClick={this.handleClick}>x</button>
<div className="overlay-content">
<SearchInput ref="searchInput" className="search-input" onChange={this.searchUpdated} />
<ul>{location}</ul>
</div>
</div>
</div>
);
}
}
最佳答案
我认为您正在寻找的是 encodeURIComponent .
search( query='kathmandu' ){
还有:
let url = 'http://localhost:8000/api/v1/rental/?place__startswith=' +encodeURIComponent(query);
注意,由于您的查询字符串实际上只包含字母,因此在该示例中您不需要 encodeURIComponent
,但在其他情况下可能需要它。
关于javascript - 如何在函数中传递不带引号的字符串作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36385243/