0

I have object main.py

from __future__ import with_statement
from flask import Flask,request,jsonify,send_file,render_template
import json
# from flask_cors import CORS

app = Flask(__name__, static_url_path='/vendor')
# CORS(app)


@app.route('/')
def home():
    return render_template('index.html',id_user="id1")


@app.route('/receive_word',methods=['POST'])
def receive_word():
    print(request.form)
    data = request.form['javascript_data']
    d = json.loads(data)
    print(d['key1'])
    print(d['key2'])
    return d

I have microphone.js

$.post("/receive_word", {
    javascript_data: JSON.stringify({ "key1":1, "key2":this.currentTranscript })
});
console.log({{ d }});

How can I pass d in my main.py to main.js? The code can't caught the d variable from main.py Thank you

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
Frieda
  • 163
  • 1
  • 1
  • 10

2 Answers2

1

You seem to use JQuery to make your call to your endpoint. You should use the $.post callback parameter :

$.post("/receive_word", {
    javascript_data: JSON.stringify({ "key1":1, "key2":this.currentTranscript })
}, 
// Here is the callback
function(d, status){
     // process d
});
0

Use jsonify to return json data in Flask view, assume that d is a valid Python dict:

@app.route('/receive_word',methods=['POST'])
def receive_word():
    data = request.form['javascript_data']
    d = json.loads(data)   
    return jsonify(d)
dvnguyen
  • 2,954
  • 17
  • 24