4
我需要從python 2.7中的serverside列表中獲取列表以打印它們的javascript前端。有沒有辦法做到這一點?我試圖通過「self.response.out.write(my_list)」來傳遞列表。從服務器獲取列表並打印被span標記包圍的每個元素
import webapp2
import jinja2
import os
import logging
from google.appengine.ext import db
import random
from google.appengine.api import users
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__) + '/template'))
class MainPage(webapp2.RequestHandler):
def get(self):
# Print webpage
template_values = {}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
def post(self):
my_list = ['one', 'two', 'three', 'four']
self.response.out.write(my_list)
app = webapp2.WSGIApplication([('/', MainPage),],debug=True)
Javascript代碼遍歷列表,並在控制檯打印出來。
function refresh(e) {
console.log('Refresh.');
addNewWord();
}
function addNewWord() {
console.log('Add new words');
$.ajax('/',{
type: 'POST',
data: {
},
success: handleResponse
});
}
function handleResponse(data) {
console.log('Got from server:' + data);
for (var i = 0; i <data.length; i++) {
console.log(data[i]);
}
}
$(document).ready(function() {
$('#refreshButton').click(refresh);
});