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

Published: 12 April 2021
on channel: ZeroLife
21k
244

//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)
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:
PP:
BTC: 3EUQBWZKX9Vcwdffd3cUGATQopxrAQQxJ9
LTC: MBXE6hJgxxFYSD8SjmR7sHCHGTqFXmNGoU
GFM:


Watch video Sending POST requests using AJAX (via JavaScript)to a Python Backend (via Flask) online without registration, duration 13 minute 12 second in high hd quality. This video was added by user ZeroLife 12 April 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 21 thousand once and liked it 244 people.