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}/>
          &nbsp;
          &nbsp;
          &nbsp;
          <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
    }