I would like to send a string from my Flask app to a.js file (not a script on HTML). I know I need to use an Ajax 'GET' Type but i am not sure on how to receive it on the.js file and console.log the data.
@app.route('/',methods =['GET','POST'])
def index():
req = json.dumps(request.get_json())
if request.method == 'POST':
result = getString(req) #Function outputs a string
print('Received')
return jsonify(result)
else:
print('Not Received')
return render_template('index.html')
if __name__ == '__main__':
app.run()
.js file (Which needs help):
$.ajax({
url: "/",
type: 'GET',
data: ???, #What should i put here
success: function(data) {
console.log(data);
}
});
Note that the following snippet implies that if the endpoint receives a GET request then nothing will be returned.
if request.method == 'POST':
result = getString(req) #Function outputs a string
print('Received')
return jsonify(result)
else:
# This is what will get called on a GET request
print('Not Received')
After you fix that, you don't need the data
field on the ajax side at all unless you want to provide some query parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.