在JSON对象中提交表单值

问题描述 投票:0回答:1

我正在研究聚合物.js。目前,我正在开发一个简单的Web应用程序来签署用户。目前,每当我提交表单时,它都会以下面显示的格式提交,而不是完整的JSON对象。

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

代替 ...

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

这是我的代码:

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

登入-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>

javascript html5 polymer
1个回答
0
投票

你应该发送一个字符串时发送一个对象(obj)。

你有没有在你要发送的对象上使用JSON.stringify(obj)

其他你没有问过的事情。


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

你应该使用this.set('email', this.$.email.value.trim()because this.set在Polymer中发送一个事件来更新值。现在这不是问题,但如果继续使用this.[property],它可以在其他元素中。


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

您可以立即将侦听器绑定到这些,以避免使用:

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

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

它看起来像:

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

我使用了两种Polymer监听器,输入和更改。你可以使用其中之一。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.