I am very new to web development back-end and my goal is to call a python script from Javascript.
Using Flask, I think I am able to host my python script on localhost port 5000 python code:
from flask import Flask
app = Flask(__name__)
@app.route('/sub', methods=['GET', 'POST'])
def my_func():
"""
I want this function to be called using javascript
"""
This is how I am trying to use ajax in JavaScript to call the function
function logResults(json){
console.log(json);
}
$.ajax({
url: "http://0.0.0.0:5000/sub",
dataType: "jsonp",
jsonpCallback: "logResults"
});
I learned that you have to use jsonp in order to have a cross domain request. (for example I need to reach port 5000 on localhost while the javascript is running on port 9000 of localhost)
When trying to run this code, I am receiving
Loading failed for the < script > with source “http://0.0.0.0:5000/sub?callback=logResults&_=1604937695311”.
Does anyone know where I could be making a mistake? Am I on the right track?
This worked for me, but does anyone know how to send a data parameter when dataType is jsonp?
$.ajax({
url: "http://localhost:5000/sub",
dataType: "jsonp",
jsonpCallback: "logResults"
});
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.