我是聚合物新手,而且我一直在努力使用iron-ajax。这是我的代码
我的组件:
<template>
<button type="submit" onclick="getPost" class="btn btn-default" id="register-form" style="margin-left: 35%; background-color: #008000; color: black; width: 30%;">Submit</button>
<iron-ajax
id="ajax"
method="POST"
body='[{"id": "1"}]'
url ="http://localhost/api/post.php"
on-response="success"
lastError="error"
debounce-duration="3000">
</iron-ajax>
</template>
<script>
Polymer({
is: 'register-form',
getPost:function(){
this.$.ajax.generateRequest();
},
success:function(r){
alert(r.detail.response);
},
error:function(r){
alert(r.detail.response);
}
});
</script>
我的php文件:
<?php
header('Access-Control-Allow-Origin: *');
if ($_POST){
echo $_POST['id'];
}
?>
最初我希望php返回一个json,但它一直在返回null。所以我决定将它改为那个,看它是否会返回我发布的id,但它仍然返回null.GET方法似乎工作虽然很好。我已经尝试过我在互联网上找到的解决方案,但它仍然有效。请帮忙。
答案 0 :(得分:0)
你能改变你的身体属性吗?
body='{"id": "1"}'
...如果上述方法无效,请尝试计算绑定。 e.g。
body="[[_body()]]"
和
_body: function() {
return {
id: 1
};
}
答案 1 :(得分:0)
按钮onclick="getPost"
未调用您组件的getPost
方法,请将其替换为on-click=...
,这是聚合物用于附加{{1}的惯例}按钮的事件监听器