Sending POST requests using AJAX (via JavaScript)to a Python Backend (via Flask)

Published: 12 April 2021
on channel: ZeroLife
21,571
249

//JavaScript Part//
var xml = new XMLHttpRequest();
xml.open("POST","{{url_for('func.func')}}",true);
xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xml.onload = function(){
var dataReply = JSON.parse(this.responseText);
};//endfunction

dataSend= JSON.stringify({
'page_data':'some_data'
});

xml.send(dataSend);


//Python Part//

import os
from flask import Flask, Blueprint, jsonify
from project import app

path_cwd = os.path.dirname(os.path.realpath(__file__))
path_templates = os.path.join(path_cwd,"templates")
path_static = os.path.join(path_cwd,"static")

Func = Blueprint('func', __name__, static_folder=path_static, template_folder=path_templates)
@Func.route('/func', methods=['GET','POST'])
def func():

dataGet = '' if not request.get_json(force=True) else request.get_json(force=True)

dataReply = {'backend_data':'some_data'}

return jsonify(dataReply)

PAT:   / rezatahirkheli  
PP: https://www.paypal.com/paypalme/rezat...
BTC: 3EUQBWZKX9Vcwdffd3cUGATQopxrAQQxJ9
LTC: MBXE6hJgxxFYSD8SjmR7sHCHGTqFXmNGoU
GFM: https://gofund.me/7d6500bd


On this page of the site you can watch the video online Sending POST requests using AJAX (via JavaScript)to a Python Backend (via Flask) with a duration of hours minute second in good quality, which was uploaded by the user ZeroLife 12 April 2021, share the link with friends and acquaintances, this video has already been watched 21,571 times on youtube and it was liked by 249 viewers. Enjoy your viewing!