Iron Ajax - How to access Response from on-response function?

走远了吗. 提交于 2019-12-05 07:10:19
tony19

The response data is in fact returned in event.detail.response of the <iron-ajax>.response event. Your response field is null because you've misconfigured <iron-ajax>.handleAs. When you set it to json, the Accept-Type header is set to application/json and any response would be parsed with JSON.parse(). If your server ignores Accept-Type and sends whatever it wants, <iron-request> will attempt to parse the response as JSON and fail, causing a null response body per the spec. Note that hello and <p>Hello</p> are not valid JSON strings.

If you want to receive plaintext data, set <iron-ajax>.handleAs to text (the default is json).

Demo of <iron-ajax handle-as="text">

Demo of <iron-ajax handle-as="json">


The question you linked asks about the <iron-ajax>.error event, which has a different event detail than the <iron-ajax>.response event.

When <iron-ajax> receives a server response, it fires the response event with the corresponding <iron-request> as the event detail.

If the request fails for any reason, <iron-ajax> fires the error event with an object (containing the iron-request via the request attribute, and the underlying error via error) as the event detail.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!