简体   繁体   English

如何响应XMLHttpRequest

[英]How to respond to an XMLHttpRequest

I'm using a Javascript to ask our app (which is in Google App Engine) if the file a user wants to upload is already in his list of files (he will overwrite). 我正在使用Javascript询问我们的应用程序(位于Google App Engine中),用户要上传的文件是否已经在其文件列表中(他将覆盖)。

I know how to send the request, but how can I create a response from the server, using Python? 我知道如何发送请求,但是如何使用Python从服务器创建响应?

This is the request: 这是请求:

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);

As you see, we are getting the responseText from the request after everything has gone OK, but my question is how do we fill that responseText field on the server side?? 如您所见,一切正常后,我们将从请求中获取responseText,但是我的问题是我们如何在服务器端填充该responseText字段?

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM