简体   繁体   English

在JSON对象中提交表单值

[英]Submit form values in JSON object

I'm working on learning polymer.js. 我正在学习Polymer.js。 At the moment I'm just working on a simple web app to sign a user in. Currently, whenever I submit my form it submits in this format shown below versus a complete JSON object. 目前,我只是在使用一个简单的Web应用程序来登录用户。目前,每当我提交表单时,它都会以下面显示的格式(而不是完整的JSON对象)提交。

{
  email: "email@email.com",
  password: "password"
}

instead of ... 代替 ...

{
  "email":"email@email.com",
  "password":"password"
}

Here is my code: 这是我的代码:

<form action="http://httpbin.org/post" method="post">

  <sign-in-input email="{{_email}}" password="{{_password}}"></sign-in-input>

  <input class = "paperbtn" type="submit" value="Sign in">

  <input name="email" value="[[_email]]" hidden>
  <input name="password" value="[[_password]]" hidden>

</form>

sign-in-input.html: sign-in-input.html:

    <dom-module id="sign-in-input">
<template>
      <paper-input label = "Email" id = "email" required></paper-input>
      <paper-input label = "Password" id = "password" required></paper-input>
</template>

<script>
  Polymer({
    is: 'sign-in-input',

    properties: {
      email: {
        type: String,
        notify: true
      },
      password: {
        type: String,
        notify: true
      }
    },

    listeners: {
      'input': '_onInput'
    },

    _onInput: function() {
      this.email = this.$.email.value.trim();
      this.password = this.$.password.value.trim();
    }
  });
</script>

You're sending an object ( obj ) when you should send a string. 您应该在发送字符串时发送对象( obj )。

Have you used JSON.stringify(obj) on the object that you want to send? 您是否在要发送的对象上使用了JSON.stringify(obj)

Other things that you're not asking about. 您没有要求的其他事项。


_onInput: function() {
  this.email = this.$.email.value.trim();
  this.password = this.$.password.value.trim();
}

You should use this.set('email', this.$.email.value.trim() because this.set sends an event in Polymer to update the value. This isn't an issue now, but it can be in other elements if you continue to use this.[property] . 您应该使用this.set('email', this.$.email.value.trim()因为this.set在Polymer中发送事件以更新值。现在这不是问题,但在其他情况下也可以元素(如果您继续使用this.[property]


  <paper-input label = "Email" id = "email" required></paper-input>
  <paper-input label = "Password" id = "password" required></paper-input>

You can bind listeners to these right away, to avoid using: 您可以立即将侦听器绑定到这些侦听器,以避免使用:

listeners: {
  'input': '_onInput'
},

_onInput: function() {
  this.email = this.$.email.value.trim();
  this.password = this.$.password.value.trim();
}

It will look something like: 它看起来像:

 <paper-input label="Email" id="email" value="{{email::input}}" required></paper-input>
 <paper-input label="Password" id="password" value="{{password::change}}" required></paper-input>

I used two kinds of Polymer listeners, input and change. 我使用了两种Polymer侦听器,即输入和更改。 You can use either. 您可以使用。

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

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