从控制台输出Javascript/Python/Jinja2保存变量
我正在使用TrelloAPI创建一张卡。这可以正常工作,但在脚本运行时,它会将新创建的卡的ID变量打印到控制台。我想获取这个变量并将其传递回python代码。这可能吗?我正在使用Jinja2将变量从Python传递到HTML,我可以在这里使用它吗 这是Chrome中控制台的输出,我想获取第一个ID变量,以便在Python代码中使用它从控制台输出Javascript/Python/Jinja2保存变量,javascript,python,Javascript,Python,我正在使用TrelloAPI创建一张卡。这可以正常工作,但在脚本运行时,它会将新创建的卡的ID变量打印到控制台。我想获取这个变量并将其传递回python代码。这可能吗?我正在使用Jinja2将变量从Python传递到HTML,我可以在这里使用它吗 这是Chrome中控制台的输出,我想获取第一个ID变量,以便在Python代码中使用它 { "id": "5a46fa28620367df83fe08f7", "badges": { "votes": 0, "attachmen
{
"id": "5a46fa28620367df83fe08f7",
"badges": {
"votes": 0,
"attachmentsByType": {
"trello": {
"board": 0,
"card": 0
}
},
"viewingMemberVoted": false,
"subscribed": false,
"fogbugz": "",
"checkItems": 0,
"checkItemsChecked": 0,
以下是我的Javascript:
var authenticationSuccess = function() {
console.log('Successful authentication');
};
var authenticationFailure = function() {
console.log('Failed authentication');
};
window.Trello.authorize({
type: 'popup',
name: 'Work Requests App',
scope: {
read: 'true',
write: 'true' },
expiration: 'never',
success: authenticationSuccess,
error: authenticationFailure
});
//console.log(trelloTitle);
//console.log(trelloContent);
var myList = '5a3ec86f7920a6e66b28e4bc';
var creationSuccess = function (data) {
console.log('Card created successfully.');
console.log(JSON.stringify(data, null, 2));
};
//var data = $('#resultsdata').data();
//console.log(trelloId);
var newCard = {
name: trelloTitle,
desc: trelloContent,
// Place this card at the top of our list
idList: myList,
idMembers: trelloId,
pos: 'top'
};
window.Trello.post('/cards/', newCard, creationSuccess);
编辑:我现在使用这个AJAX方法:
var creationSuccess = function (data) {
console.log('Card created successfully.');
console.log(JSON.stringify(data, null, 2));
var url = "/ajaxtest";
$.post(url, {card_id: data.id});
};
我很难将card_id传递到python方法中:
class AjaxTest(webapp2.RequestHandler):
def post(self):
data = card_id
我知道这是错误的,有人能帮忙吗?以下是完整的答案:
var creationSuccess = function (data) {
console.log('Card created successfully.');
var http = new XMLHttpRequest();
var url = "URL to your server (where you want to send the data)";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send("card_id=" + data.id);
};
这将通过AJAX调用将卡id作为card\u id
发送到您的服务器(到您指定的URL)
这是假设您只使用纯Javascript。如果您使用的是jQuery,那么进行AJAX调用就会容易得多。比如:
var creationSuccess = function (data) {
console.log('Card created successfully.');
var url = "URL to your server";
$.post(url, {card_id: data.id});
};
编辑:对于服务器代码,您现在可以接收如下卡id
:
class AjaxTest(webapp2.RequestHandler):
def post(self):
data = self.request.get('card_id')
希望这会有所帮助。您似乎在
creationSuccess
函数中将JSON对象打印到控制台。将数据发送到您的Python服务器会帮您完成这项工作吗?是的,我不知道如何从JSON中获取所需内容并将其传递回Python代码。您应该能够使用data.id
访问id。然后通过AJAX调用将其发送到服务器。谢谢,我以前没有使用过AJAX,您能提供一个示例吗?您使用的是纯Javascript?没有使用jQuery或类似的库?谢谢!我想我已经基本做到了,我已经更新了我的帖子来展示我的python代码-我不知道如何将card\u id
传递到我的python方法中,你介意看一下吗?无论如何,我都会将此标记为答案。我正在使用Webapp2。谢谢您的帮助!