Ajax 如何使用Polymer发布JSON
我用polymer做了一个地籍屏幕,我想发布一个带有用户、密码和电子邮件的JSON。我知道在聚合物中有核心ajax可以做到这一点,但我不知道如何才能做到这一点。如果有人能帮我。代码如下:Ajax 如何使用Polymer发布JSON,ajax,json,post,polymer,Ajax,Json,Post,Polymer,我用polymer做了一个地籍屏幕,我想发布一个带有用户、密码和电子邮件的JSON。我知道在聚合物中有核心ajax可以做到这一点,但我不知道如何才能做到这一点。如果有人能帮我。代码如下: <div class = "cadastro"> <template id="user-maintenance" is="auto-binding"> <div> <paper-input-decorator id="user" label="Usuári
<div class = "cadastro">
<template id="user-maintenance" is="auto-binding">
<div>
<paper-input-decorator id="user" label="Usuário" floatinglabel type="text" value="{{user}}" error="Usuário deve conter apenas letras e no mímino 3">
<input id="user1" type="text" is="core-input" pattern="^[A-Za-z*]{3,}$" required>
</paper-input-decorator >
</div>
要做一个表格,我首先制作一个聚合物元素,在这个例子中,我将它称为我的表格。在该元素中,我们将创建一个创建的函数,用于设置发送到服务器的对象。下面是一个只有1个输入字段的简单版本,但应该可以让您了解它的工作原理
<polymer-element name="my-form">
<template>
<div id="invalid">
<paper-input-decorator
id="check"
floatinglabel
error="Error!!!"
label="LABEL">
<input is="core-input" id="data" required committedValue="{{item.firstItem}}" pattern="^[A-Za-z0-9]+$">
</paper-input-decorator>
<br>
<paper-button
on-tap="{{checkData}}"
raised>
Save
</paper-button>
</div>
<core-ajax
id="ajax"
auto="false"
method="POST"
url="URL"
handleas="json"
params='{{item}}'
response="{{response}}">
</core-ajax>
<paper-toast id="toast"></paper-toast>
</template>
<script>
Polymer('my-form', {
created: function () {
// sets up the object
this.item = {};
},
checkData: function () {
// this function will check data of all inputs in the invalid div to make sure it matches data type and pattern
var $d = this.$.invalid.querySelectorAll('paper-input-decorator'),
that = this;
Array.prototype.forEach.call($d, function (d) {
d.isInvalid = !d.querySelector('input').validity.valid;
});
// the timeout gives time to mark as invalid before checking
setTimeout(function () {
// for each input you are checking you will need another variable and also to add the check to the following if statment
var invalid = that.$.check.classList.contains("invalid");
if (!invalid) {
that.$.ajax.go();
}
}, 100);
},
responseChanged: function () {
// do something on response i like to use a paper-toast here alerting the user the data has been accepted or rejected
this.$.toast.text = response;
this.$.toast.show();
}
});
</script>
</polymer-element>
我试着用我的代码做类似的事情,但不起作用,我在google designer中试过你的代码,但也不起作用。我对这一点很陌生,我不明白这为什么不起作用。我回顾了我的代码,发现在分号应该出现的地方有一个冒号。此外,如果您没有导入此代码中使用的html元素,它将无法工作。我认为我的回答是含蓄的,所以没有包括在内。我用你可以在这里找到的工作代码制作了一个插件,非常感谢,我在第一次导入了html元素。但是当看到代码运行时,我能够理解。谢谢