English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Sijax stands for 'Simple Ajax', which is a Python/The jQuery library is designed to help Flask developers easily integrate Ajax into their applications. It uses jQuery.ajax to send AJAX requests.
Flask-The installation of Sijax is very simple, use the following command -
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 pip install flask-sijax
configuration
SIJAX_STATIC_PATH - Sijax JavaScript file static path. The default location is static/js/sijax. In this folder, sijax.js and json2.js file. SIJAX_JSON_URI - loading json2.js static file URI
Sijax uses JSON to pass data between the browser and the server. Therefore, the browser needs local support for JSON or from json2.js file to obtain JSON support.
Functions registered in this way cannot provide Sijax functionality because, by default, they cannot be accessed via the POST method (and Sijax uses POST requests).
To enable the View function to handle Sijax requests, you can use @app.route('/url', methods = ['GET', 'POST']) can be accessed via POST, or use similar @flask_sijax.route auxiliary decorator -
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 @flask_sijax.route(app, '')/hello())
Each Sijax handler function (like this) automatically receives at least one parameter, just like Python passes 'self' to object methods. The 'obj_response' parameter is the object returned by the function to the browser.
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 def say_hi(obj_response): obj_response.alert('Hi sijax!')
When a Sijax request is detected, Sijax handles it like this -
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 g.sijax.register_callback('say_hi', say_hi) return g.sijax.process_request()
The smallest Sijax application code is as follows -
# Filename: example.py # Copyright : 2020 By w3codebox # Author by: www.oldtoolbag.com # Date : 2020-08-08 import os from flask import Flask, g from flask_sijax import sijax path = os.path.join('.', os.path.dirname(__file__), 'static'/js/sijax/') app = Flask(__name__) app.config['SIJAX_STATIC_PATH'] = path app.config['SIJAX_JSON_URI'] = ''/static/js/sijax/json2.js' flask_sijax.Sijax(app) @app.route('/') def index(): return 'Index' @flask_sijax.route(app, '')/hello()) def hello(): def say_hi(obj_response): obj_response.alert('Hi there!') if g.sijax.is_sijax_request: # Sijax request detected - let Sijax handle it g.sijax.register_callback('say_hi', say_hi) return g.sijax.process_request() return _render_template('sijaxexample.html') if __name__ == '__main__': app.run(debug=True)
When a Sijax requests the server (a special jQuery.ajax() request), this request is detected by g.sijax.is_sijax_request() on the server, and in this case, Sijax will automatically handle the request.
All functions registered with g.sijax.register_callback() are exposed to the browser for calling.
Call g.sijax.process_request() to tell Sijax to execute the appropriate (pre-registered) function and return the response to the browser.