[英]How to get response from an iron-form Polymer 1 request
i'm trying to get the response from an iron-form in Polymer 1
. 我试图从iron-form in Polymer 1
的iron-form in Polymer 1
得到响应。
Form submit call a php script which return HTML code to insert in a div
( ul
and some li
). 表单提交调用一个PHP脚本,它返回HTML代码插入div
( ul
和一些li
)。
I use the iron-form
event " iron-form-response
" but i don't know how to get the response. 我使用iron-form
事件“ iron-form-response
”,但我不知道如何得到响应。
I can see the response in network tab of browser developer tools, but don't know how to get it in my element. 我可以在浏览器开发人员工具的网络选项卡中看到响应,但不知道如何在我的元素中获取它。
I don't find how to do in the iron-form
documentation online . 我没有在网上找到iron-form
文档中的怎么做。
Can someone help me please ? 有谁可以帮助我吗 ?
What's happening, guys? 发生了什么,伙计们? All these responses confuse the OP when it is only this simple: 只有这么简单时,所有这些反应都会混淆OP:
Your form: 你的表格:
<form is="iron-form" on-iron-form-response="responseHandler" action="http://localhost" id="myform">
<!-- Your form elements -->
</form>
Your script: 你的脚本:
<script>
Polymer({
// Some scripts here.
// ...now your listener
responseHandler: function(e) {
console.log(e.detail.response);
},
});
</script>
It's just that. 就是这样。 Nothing complicated. 没什么复杂的。 Don't over-complicate things. 不要过于复杂。
Add Event Listeners to iron form. 将事件监听器添加到铁形式。
ready: function(){
this.$.myform.addEventListener('iron-form-response',this.formResponse);
this.$.myform.addEventListener('iron-form-error',this.formError);
}
Form Response Function: 表格响应功能:
formResponse: function (e){
console.log("Server Response: ",e.detail);
}
Form Error Function: 表单错误功能:
formError: function (e){
console.log("Form Error: ",e.detail);
}
I'm going to build off of Talon's answer, which is correct. 我要建立Talon的答案,这是正确的。
e.detail
will be a JSON object, assuming the response sent from the server is in JSON form. 假设从服务器发送的响应是JSON格式, e.detail
将是一个JSON对象。 So, if you're using Node.JS and Express, you might have receiving code like this: 所以,如果你使用Node.JS和Express,你可能会收到这样的代码:
document.getElementById('my-form').addEventListener('iron-form-response', function (e) {
console.log('Form :', e.detail);
});
And your server code might look like this: 您的服务器代码可能如下所示:
res.status(200).json({'foo': 'bar'});
After which e.detail
will be the object {"foo": "bar"}
之后e.detail
将成为对象{"foo": "bar"}
Small update. 小更新。 I send some json with response: 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"});
res.contentType('json'); es.status(500).send({"foo":"bar"});
If I use 500 (error), I can reach the json data only by console.log(e.detail.request.xhr.response);
如果我使用500(错误),我只能通过console.log(e.detail.request.xhr.response);
访问json数据console.log(e.detail.request.xhr.response);
In case of code 200 it is reached by: console.log(e.detail.response);
在代码200的情况下,它通过以下方式到达: console.log(e.detail.response);
Idon't get why it is so, but it's the only way for me(( 我不明白为什么会这样,但这对我来说是唯一的方式((
<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.