提交前修改铁型JSON

提交前修改铁型JSON

本文介绍了提交前修改铁型JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在提交铁表格之前编辑它的JSON文件?例如,如果我想添加一个不是来自任何表单输入的数组,或者我想在其中添加唯一键...

I'm wondering if it is possible to edit the JSON file of an iron-form before submitting it? For instance, if I want to add an array which doesn't come from any of the form input, or if I want to add a unique key in it...

如果可能的话,我相信会在表单提交前,但是文档没有说明如何截取JSON"或类似内容.

If it is possible, I believe it would be during the form pre-submit, but the documentation says nothing about "how to intercept the JSON" or something like that.

谢谢!

推荐答案

您可以修改iron-form的对象.对于POST请求,表单数据存储在主体中,您可以通过this.$.form.request.body访问该主体.对于其他请求类型,数据在this.$.form.request.params中.此示例将数组添加到请求的正文中:

You could modify the iron-form's request object in the iron-form-presubmit event handler. For POST requests, the form data is stored in the body, which you would access by this.$.form.request.body. For other request types, the data is in this.$.form.request.params. This example adds an array to the request's body:

// template
<form is="iron-form" id="form" on-iron-form-presubmit="_presubmit" method="post">...</form>

// script
_presubmit: function(e) {
  var body = this.$.form.request.body;
  body['newkey'] = [1,2,3];
  console.log('body', body);
},
<head>
  <base href="https://polygit.org/polymer+1.11.3/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-form/iron-form.html">
  <link rel="import" href="paper-input/paper-input.html">
  <link rel="import" href="paper-button/paper-button.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <form is="iron-form" id="form" on-iron-form-presubmit="_presubmit" method="post" action="//httpbin.org/post">
        <paper-input name="name" label="name"></paper-input>
        <paper-button on-tap="_submit">Submit</paper-button>
      </form>
    </template>
    <script>
      HTMLImports.whenReady(function() {
        Polymer({
          is: 'x-foo',
          _presubmit: function(e) {
            var body = this.$.form.request.body;
            body['newkey'] = [1,2,3];
            console.log('body', body);
          },
          _submit: function() {
            this.$.form.submit();
          }
        });
      });
    </script>
  </dom-module>
</body>

这篇关于提交前修改铁型JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:31