[英]Send data from javascript to python
I use the below code to send data (GET) from python to javascript. 我使用以下代码将数据(GET)从python发送到javascript。
in javascript: 在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: 在python中:
channels_str = channels.getvalue()
data['channel'] = channels_str
return dumps(data)
how can I send data from javascript to python? 如何将数据从javascript发送到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. 您请求扫描 ,但是如果添加“?who = Masha”,则会发送一个参数谁 ,其值为“ Masha”作为获取请求参数。
$.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: 然后,如果您的python get响应(通常为def do_GET(self):
具有:
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) 请注意,您需要导入urlparse (在Python3中,您将使用urllib.parse)
The GET Method GET方法
Note that the query string (name/value pairs) is sent 请注意,查询字符串(名称/值对)已发送
in the URL of a GET request: 在GET请求的网址中:
/test/demo_form.asp?name1=value1&name2=value2
Some other notes on GET requests: 有关GET请求的其他注意事项:
- GET requests can be cached 可以缓存GET请求
- GET requests remain in the browser history GET请求保留在浏览器历史记录中
- GET requests can be bookmarked GET请求可以加书签
- GET requests should never be used when dealing with sensitive data 处理敏感数据时,切勿使用GET请求
- GET requests have length restrictions GET请求有长度限制
- GET requests should be used only to retrieve data GET请求应仅用于检索数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.