我正在学习聚合物。我无法计算使用<iron-ajax>
. 我正在使用在线测试 API ( https://reqres.in/ ),我应该收到此响应,状态码为 200:
{"token": "QpwL5tke4Pnpja7X"}".
我找不到显示POST
示例的教程。过去 24 小时我一直在网上搜索,但一切都是关于GET
而不是POST
。
如果任何熟悉的人都<iron-ajax>
可以查看我的代码并帮助我使其工作或弄清楚如何编写正确的代码,这对像我这样的初学者将非常有帮助。
- 我的代码与该
body
属性是否正确? 响应是
200
状态码还是令牌?<!-- @license Copyright (c) 2016 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as part of the polymer project is also subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt --> <link rel="import" href="../bower_components/polymer/polymer-element.html"> <link rel="import" href="shared-styles.html"> <link rel="import" href="../bower_components/polymer/polymer.html"> <link rel="import" href="../bower_components/iron-ajax/iron-ajax.html"> <dom-module id="my-view2"> <!--test using this data: { "email": "peter@klaven", "password": "cityslicka" }--> <template> <iron-ajax> auto method="post" url="https://reqres.in/api/login" handle-as="json" content-type="application/json" body =[{"email": "peter@klaven", "password": "cityslicka"}] on-response={{handleResponse}} </iron-ajax> <!--Handle response--> <p> response handling code goes here, how to show the response from the server here?</p> <p> It should show: {"token": "QpwL5tke4Pnpja7X"} </p> <div> <p> {{handleResponse}} </p> </div> </template> <script> class MyView2 extends Polymer.Element { static get is() { return 'my-view2'}; static get proporties() { return { handleResponse: { type: Object, notify: true, readOnly: true } }; } } window.customElements.define(MyView2.is, MyView2); </script> </dom-module>