我正在使用Javascript来询问我们的应用(在Google App Engine中),如果用户想要上传的文件已经在他的文件列表中(他将覆盖)。
我知道如何发送请求,但是如何使用Python从服务器创建响应?
这是请求:
var req = new XMLHttpRequest();
req.open('POST', 'https://safeshareapp.appspot.com/upload', async);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", body.length);
req.setRequestHeader("Connection", "close");
if (async) {
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
var response = null;
try {
response = JSON.parse(req.responseText);
} catch (e) {
response = req.responseText;
}
callback(response);
}
}
}
// Make the actual request
req.send(body);
如您所见,我们在所有内容都正常后从请求中获取responseText,但我的问题是我们如何在服务器端填充responseText字段?
答案 0 :(得分:6)
class MyRequestHandler(webapp.RequestHandler):
def get(self):
import json
result = {"filename": xxx} // just an example, result can be any Python object
json_obj = json.dumps(result)
self.response.out.write(str(json_obj))