本文介绍了DART& GAE:为什么POST方法从dart发送不能在GAE中求值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Dart代码用于发送带有POST方法的HttpRequest到我的GAE WepApp2应用程序。 dart代码在chrome中执行,并由Chrome开发人员编辑。我在我的GAE代码中添加了一些头,以避免客户端的XHR错误。



dart代码将数据发送到我的GAE应用程序,数据与 self.request.POST.get(语言)),应用程序从不输入 def post(self):部分,但 self.request .body 我可以读取数据。



您可以解释一下,并提供一些更正以获得完整的POST符合性代码吗?



dart:

  _saveData(){
HttpRequest request = new HttpRequest(); //创建一个新的XHR

//添加一个事件处理程序,当请求结束时调用
request.onReadyStateChange.listen((_){
if(request.readyState == HttpRequest.DONE&&
(request.status == 200 || request.status == 0)){
// data saved OK。
print(request.responseText );
}
});

//将数据发送到服务器
var url =http://127.0.0.1:8080/savedata;
request.open(POST,url,async:false);

String jsonData = JSON.encode({language:dart});
request.send(jsonData);
}

我的处理程序中的GAE代码:

  def savedata(self):
logging.info(test)
logging.info
logging.info(self.request.POST.get(language))
def post(self):
logging.info(test 2)
logging.info(self.request.POST.get(language))

self.response.headers [Access-Control-Allow-Origin] =http://127.0。 0.1:49981
self.response.headers [Access-Control-Allow-Methods] =POST,GET,OPTIONS

在Dart中,如果你没有指定 request.setRequestHeader(Content-Type,application / x-www- form-urlencoded),那么数据就像一个bite流一样被GAE考虑,您只能使用 self.request.body 来读取它们



如果在Dart中添加Content-Type头,还需要更改数据。在我的情况下,我模仿一个表单发送POST方法,所以我改变 String jsonData = JSON.encode({language:dart}); 通过 String jsonData =language = dart2 ;



在GAE中python现在,我可以使用 self.request.POST.get(language)



如果您需要将JSON从DART发送到GAE,则可以对字符串进行编码,如下所示:

  String jsonData = JSON.encode({test:valuetest1}); 
String datas =datas = $ jsonData;
request.send(datas);

在GAE中,您可以这样读取数据:

  my_json = json.loads(self.request.POST.get(datas))
logging.info(my_json [ test])

完整代码:



Dart

  void _saveData2(){
String url =http://127.0.0.1:8080/savedata;
HttpRequest request = new HttpRequest()
..open(POST,url,async:true)
..setRequestHeader(Content-Type,application / x-www- form-urlencoded)
..responseType =arraybuffer;

String jsonData = JSON.encode({test:valuetest1});
String datas =datas = $ jsonData;
request.send(datas);
}

GAE

  class PageHandler(webapp2.RequestHandler):

def savedata(self):

self.response.headers.add_header('Access-Control-Allow-Origin','*')
self.response.headers ['Content-Type'] ='application / json'

#logging.info(self.request)
my_json = json.loads(self.request.POST.get(datas))
logging.info(my_json [test ])


I have a Dart code used to send an HttpRequest with a POST method to my GAE WepApp2 application. The dart code is executed in chromium and serve by Chrome dev editor. I add in my GAE code some headers to avoid the XHR error in the client side.

The dart code send the datas to my GAE app but I can't read the data with self.request.POST.get("language")) and the app never enter in def post(self): section but with self.request.body I can read the data.

Could you explain that and provide some correction to have a full POST compliant code?

dart:

void _saveData() {
    HttpRequest request = new HttpRequest(); // create a new XHR

    // add an event handler that is called when the request finishes
    request.onReadyStateChange.listen((_) {
      if (request.readyState == HttpRequest.DONE &&
          (request.status == 200 || request.status == 0)) {
        // data saved OK.
        print(request.responseText);
      }
    });

    // POST the data to the server
    var url = "http://127.0.0.1:8080/savedata";
    request.open("POST", url, async: false);

    String jsonData = JSON.encode({"language":"dart"});
    request.send(jsonData);
  }

GAE code in my handler:

  def savedata(self):
    logging.info("test")
    logging.info(self.request.body)
    logging.info(self.request.POST.get("language"))
    def post(self):
      logging.info("test 2")
      logging.info(self.request.POST.get("language"))

    self.response.headers["Access-Control-Allow-Origin"] = "http://127.0.0.1:49981"
    self.response.headers["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
解决方案

In Dart, if you don't specify request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") in your HttpRequest, the data is considered by GAE like a bite stream and you can only read them with self.request.body

If you add the Content-Type header in Dart you need also to change the data formating. In my case I mimic a form sending with POST method so I change String jsonData = JSON.encode({"language":"dart"}); by String jsonData = "language=dart2";

IN GAE python I can now read the data with self.request.POST.get("language")

If you need to send a JSON from DART to GAE, you can encode the string like this:

String jsonData = JSON.encode({"test":"valuetest1"});
String datas = "datas=$jsonData";
request.send(datas);

In GAE you can read the datas like this:

my_json = json.loads(self.request.POST.get("datas"))
logging.info(my_json["test"])

The complete code:

Dart

void _saveData2() {
    String url = "http://127.0.0.1:8080/savedata";
    HttpRequest request = new HttpRequest() 
      ..open("POST", url, async: true)
      ..setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
      ..responseType = "arraybuffer";

    String jsonData = JSON.encode({"test":"valuetest1"});
    String datas = "datas=$jsonData";
    request.send(datas);
  }

GAE

class PageHandler(webapp2.RequestHandler):

  def savedata(self):

    self.response.headers.add_header('Access-Control-Allow-Origin', '*')
    self.response.headers['Content-Type'] = 'application/json'

    #logging.info(self.request)
    my_json = json.loads(self.request.POST.get("datas"))
    logging.info(my_json["test"])

这篇关于DART& GAE:为什么POST方法从dart发送不能在GAE中求值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:35