【发布时间】:2012-01-17 08:46:58
【问题描述】:
我正在使用 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 字段??
【问题讨论】:
-
你使用什么语言,服务器端?假设您正在使用 PHP。然后,您可以使用:
<?php ...........; echo json_encode($variable_to_serialize); ?>
标签: javascript python google-app-engine xmlhttprequest