I use the below code to send data (GET) from python to javascript.
in javascript:
$.get('http://localhost:5000/api/scan').success(function(res) {
obj = JSON.parse(res);
if(obj['channel'] == "1"){
document.getElementById("channelZero").innerHTML = "1";
}});
in python:
channels_str = channels.getvalue()
data['channel'] = channels_str
return dumps(data)
how can I send data from javascript to python?
In case
$.get('http://localhost:5000/api/scan').success(function(res) {
//your code
}});
you request scan , but if you add "?who=Masha" you send a parameter who with a value "Masha" as get request parameter.
$.get('http://localhost:5000/api/scan?who=Masha').success(function(res) {
//your code
}});
Then if your python get response (typically def do_GET(self):
) has:
data['channel'] = urlparse.parse_qs(urlparse.urlparse(self.path).query).get('who', None)
Note that you need to import urlparse (In Python3, you'd use urllib.parse)
From w3schools
The GET Method
Note that the query string (name/value pairs) is sent
in the URL of a GET request:
/test/demo_form.asp?name1=value1&name2=value2
Some other notes on GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should never be used when dealing with sensitive data
- GET requests have length restrictions
- GET requests should be used only to retrieve data
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.