Here you really need an API to access the data. If you are already using a jar then Flask-Restful is a great option. You can configure the route as follows:
from flask import Flask from flask_restful import Resource, Api, reqparse app = Flask(__name__) api = Api(app) class Data(Resource): def __init__(self): self.parser = reqparse.RequestParser() self.parser.add_argument('name', type=str, required = True) self.parser.add_argument('whatever', type=str, required = True) def post(self): args = self.parser.parse_args()
and then you can pass in variables that indicate what data is needed to use JSON, query strings, or a form. Flask-Restful is pretty smart and will detect the method you use to send it automatically. You can then return the dictionary to be encoded using JSON. This should solve your second problem of passing multiple variables back to javascript.
An example of the corresponding javascript:
var xhr = new XMLHttpRequest(); xhr.open("POST", "http://127.0.0.1:5000/data"); xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); xhr.onload = function() { var dict = JSON.parse(xhr.responseText); console.log(dict['data']); console.log(dict['other_information']); }; var request = JSON.stringify({'name':'my_name', 'whatever':'stuff'}); xhr.send(request);
source share