Python 使用React+;用Solr搜索的烧瓶
我正在尝试使用Python 使用React+;用Solr搜索的烧瓶,python,reactjs,flask,solr,Python,Reactjs,Flask,Solr,我正在尝试使用React和Flask进行Solr搜索, 我创建了一个非常简单的JSON并将其上传到Solr核心,现在我尝试将用户输入从React发送到Flask,然后通过React返回Jsonify答案,这是我的Flask代码: from flask import Flask, request, jsonify import pysolr app = Flask(__name__) solr = pysolr.Solr('http://localhost:8983/solr/dataID
React
和Flask
进行Solr
搜索,
我创建了一个非常简单的JSON
并将其上传到Solr
核心,现在我尝试将用户输入从React
发送到Flask
,然后通过React
返回Jsonify
答案,这是我的Flask
代码:
from flask import Flask, request, jsonify
import pysolr
app = Flask(__name__)
solr = pysolr.Solr('http://localhost:8983/solr/dataID')
@app.route('/search', methods=['POST'])
def search_results():
if request.method == 'POST':
searchItem = request.args.get('searchItem', None)
results = solr.search(searchItem)
if results:
return jsonify(results)
else:
return "No Results"
这是我的React
:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { isCompositeComponentWithType } from 'react-dom/test-utils';
function App() {
return (
<div className="App">
<header>
<MainView />
</header>
</div>
);
}
class MainView extends Component {
constructor(props){
super(props)
this.state = {
searchItem: '',
results: {},
showResults: false
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(event){
this.setState({searchItem: event.target.value});
}
handleSubmit(event){
fetch('/search', {
method:"POST",
cache: "no-cache"
}).then(response => {
return response.json()
}).then(json => {
if ( !(Object.keys(json).length === 0 ) ){
this.setState({results: JSON.parse(json), showResults: true});
}
})
event.preventDefault();
}
render(){
return(
<div className="MainView">
<form onSubmit={this.handleSubmit}>
<input type='text' name="searchItem" style={{ width:"300px"}} value={this.state.searchItem} onChange={this.handleChange}/>
<input type='submit' />
</form>
{this.state.showResults &&
<div>
{this.state.results.map(item =>
<div>
<li>{item.id}</li>
<li>{item.title}</li>
<li>{item.rating}</li>
<li>{item.review}</li>
</div>
)}
</div>
}
</div>
);
}
}
export default App;
{
"id" : 2,
"title" : "title_2",
"rating" : 6.0,
"reviews" : 3371
}