繁体   English   中英

如何从铁形式的Polymer 1请求中获得响应

[英]How to get response from an iron-form Polymer 1 request

我试图从iron-form in Polymer 1iron-form in Polymer 1得到响应。

表单提交调用一个PHP脚本,它返回HTML代码插入divul和一些li )。

我使用iron-form事件“ iron-form-response ”,但我不知道如何得到响应。

我可以在浏览器开发人员工具的网络选项卡中看到响应,但不知道如何在我的元素中获取它。

我没有在网上找到iron-form文档中的怎么做。

有谁可以帮助我吗 ?

发生了什么,伙计们? 只有这么简单时,所有这些反应都会混淆OP:

你的表格:

<form is="iron-form" on-iron-form-response="responseHandler" action="http://localhost" id="myform">
   <!-- Your form elements -->
</form>

你的脚本:

<script>
   Polymer({
       // Some scripts here.

       // ...now your listener
       responseHandler: function(e) {
          console.log(e.detail.response);
       },                          
   });
</script>

就是这样。 没什么复杂的。 不要过于复杂。

将事件监听器添加到铁形式。

ready: function(){
      this.$.myform.addEventListener('iron-form-response',this.formResponse);
      this.$.myform.addEventListener('iron-form-error',this.formError);
}

表格响应功能:

formResponse: function (e){
                    console.log("Server Response: ",e.detail);
}

表单错误功能:

formError: function (e){
                    console.log("Form Error: ",e.detail);
}

我要建立Talon的答案,这是正确的。

假设从服务器发送的响应是JSON格式, e.detail将是一个JSON对象。 所以,如果你使用Node.JS和Express,你可能会收到这样的代码:

document.getElementById('my-form').addEventListener('iron-form-response', function (e) {
    console.log('Form :', e.detail);
});

您的服务器代码可能如下所示:

res.status(200).json({'foo': 'bar'});

之后e.detail将成为对象{"foo": "bar"}

小更新。 我发送一些带有响应res.contentType('json'); es.status(500).send({"foo":"bar"});res.contentType('json'); es.status(500).send({"foo":"bar"}); res.contentType('json'); es.status(500).send({"foo":"bar"}); 如果我使用500(错误),我只能通过console.log(e.detail.request.xhr.response);访问json数据console.log(e.detail.request.xhr.response); 在代码200的情况下,它通过以下方式到达: console.log(e.detail.response); 我不明白为什么会这样,但这对我来说是唯一的方式((

  <script>
    Polymer({
      is: 'rsvp-wedding',
      attached: function() {
        var form = document.querySelector('form');
        form.addEventListener('iron-form-error', function(e) {
          console.log(e.detail.request.status);
        });
      }
    });
  </script>

暂无
暂无

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

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