[英]How to send datas from python (flask) to javascript?
Hello and happy new year everybody! 大家好,新年快乐!
I have a practical problem: On my GUI I ask the user to specify a new document name with prompt method when he clicks on "Save As New button" 我遇到一个实际问题:在我的GUI上,当用户单击“另存为新按钮”时,我要求用户使用提示方法指定新的文档名称。
$("#btnSaveNew").on( "click", function() {
var newScenarioName = prompt("Please enter new scenario name", "scenarioX");
$.ajax({
url : 'http://' + document.domain + ':' + location.port + "/saveAsNew",
type : 'POST',
data : JSON.stringify({'data':data,'newName': newScenarioName}),
contentType: 'application/json;charset=UTF-8',
success: function (result) {
loadScenario();
},
});
My python-flask part ask a cloudant DB to send the file ID matching the specified name, if the name doesn't exist it creates the doc according to the metaData content but if it exists, I have no solution to warn the user 我的python Flask部分要求cloudant数据库发送与指定名称匹配的文件ID,如果名称不存在,它将根据metaData内容创建文档,但是如果存在,我没有解决办法来警告用户
newScenarioName = request.json["newName"]
file_id = getscenario(newScenarioName)
my_doc = my_db[file_id]
metaData = {
'name': newScenarioName,
'scenario': data
}
my_doc = my_db.create_document(metaData)
So I'd like to implement something like that 所以我想实现这样的事情
newScenarioName = request.json["newName"]
file_id = getscenario(newScenarioName)
my_doc = my_db[file_id]
if my_doc['name'].exist():
scenario_exist = True
******************
** missing part **
******************
else:
metaData = {
'name': newScenarioName,
'scenario': data
}
my_doc = my_db.create_document(metaData)
where the ** missing part ** includes a send instruction to the .js part that modify a var that I can add so i can make a condition such as 其中**缺少的部分**包括向.js部分发送的指令,这些指令修改了我可以添加的变量,这样我就可以做出诸如
var newScenarioName = prompt("Please enter new scenario name", "scenarioX");
if (newScenarioName === null || scenario_exist === True) {
newScenarioName = prompt("Please enter new scenario name, previous one was null or already used", "scenarioX");
}
and abort the new scenario creation! 并中止新方案的创建!
I saw a possible solution on: How to pass variable from python to javascript making a jQuery call, but the thing is, I didn't code the HTML/web-js part so I'm kinda lost in the jQuery possibilities (and with the very technical, not web-noob-friendly manual) 我看到了一个可能的解决方案: 如何将变量从python传递到javascript ,以进行jQuery调用,但问题是,我没有编写HTML / web-js部分,因此我有点迷失了jQuery的可能性(以及技术性很强,而不是网络友好的手册)
So is there a way on python side to make a method like request.json['key'] but that goes other way (post.json['key'] ? :p) Or maybe call the value .js side with an jQuery/Ajax method? 因此,在python方面是否有一种方法可以制作类似request.json ['key']的方法,但又可以做到另一种方式(post.json ['key']?:p)或用jQuery调用.js值/ Ajax方法?
Thank you for your patience and for reading this issue and sorry for the grammar fault 感谢您的耐心配合和阅读此问题,对于语法错误,我们深表歉意
EDIT: I think the only way to do so is to use websockets, the use of templates with Jinja seems a bit technically too much for me and it's more a bypass than a solution 编辑:我认为这样做的唯一方法是使用websockets,在Jinja中使用模板在技术上对我来说似乎有点过多,它比解决方案更能绕开
Ok I just bypass my problem by redefining my ajax method, it calls back the function in case of error 好吧,我只是通过重新定义我的ajax方法来绕过我的问题,如果出现错误,它会回调该函数
function saveNewHandler() {
var newScenarioName = prompt("file name");
$.ajax({
url : 'http://' + document.domain + ':' + location.port + "/saveAsNew",
type : 'POST',
data : JSON.stringify({'path':scenarioFile, 'data':data, 'name': scenarioName, 'newName': newScenarioName}),
contentType: 'application/json;charset=UTF-8',
success: function (result) {
loadScenario();
}
})
.error(function(xhr, status, error) {
console.log(xhr.responseText);
saveNewHandler(); // ask for new file name
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.