我正在使用Trello创建一个卡片。这很好,但是当脚本运行时,它会将新创建的卡片的ID变量打印到控制台。我想把这个变量传递回我的python代码。这个是可能的吗?我正在使用Jinja2将变量从Python传递到HTML,这里可以使用这个吗?
这是Chrome中控制台的输出,我想获取第一个ID变量,以便在我的Python代码中使用它。
{
"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我知道这不对,有人能帮忙吗?
发布于 2017-12-30 03:22:09
以下是完整的答案:
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_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});
};编辑:至于服务器代码,您现在可以像这样接收card_id:
class AjaxTest(webapp2.RequestHandler):
def post(self):
data = self.request.get('card_id')希望这能有所帮助。
https://stackoverflow.com/questions/48030474
复制相似问题