我有这个元素:
<template>
...
<iron-ajax
id="ajax"
url="..."
handle-as="json"
verbose=true
last-response={{ajaxResponse}}
loading="{{cargando}}"
on-response="_handleResponse">
</iron-ajax>
<div id="resultado"></div>
</template>
<script>
Polymer({
...
_handleResponse: function(event){
console.log("_handleResponse... ");
// this.$.resultado.innerHTML = event.detail.innerHTML;
}
});
</script>
我在 Firebug 中看到的响应是:
<p>Hello word</p>
我想访问以下回复_handleResponse
函数以便将其设置为innerHTML
of the resultado
div,但没有任何作用。
我努力了:
event.detail.innerHTML
event.detail.response
event.detail.xhr.response
event.detail.xhr.responseText
-
event.detail.request.xhr.response
(这条路线不存在,怎么解决Polymer Iron Ajax - 如何在错误事件后访问请求的响应? https://stackoverflow.com/questions/35220177/polymer-iron-ajax-how-to-access-response-from-request-after-error-event?)
如果我在响应函数中调试并观察 e.detail.response 值:
在网络选项卡中,我可以看到响应(简单的“你好”):
响应数据实际上返回在event.detail.response
of the <iron-ajax>.response https://elements.polymer-project.org/elements/iron-ajax#event-response事件。你的response
场是null
因为你配置错误<iron-ajax>.handleAs https://elements.polymer-project.org/elements/iron-ajax#property-handleAs。当您将其设置为json
, the Accept-Type
标头设置为application/json
任何响应都会被解析为JSON.parse() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse。如果你的服务器忽略Accept-Type
并发送它想要的任何内容,<iron-request>
将尝试将响应解析为 JSON 并失败,从而导致null响应体 https://github.com/PolymerElements/iron-ajax/blob/c6c5c80/iron-request.html#L341根据spec https://xhr.spec.whatwg.org/#response-body。注意hello
and <p>Hello</p>
不是有效的 JSON 字符串。
如果要接收明文数据,请设置<iron-ajax>.handleAs
to text
(默认是json
).
Demo of <iron-ajax handle-as="text"> http://codepen.io/tony19/pen/worxQb?editors=1010
Demo of <iron-ajax handle-as="json"> http://codepen.io/tony19/pen/bBoWVr?editors=1010
-
event.detail.request.xhr.response
(这条路线不存在,怎么解决Polymer Iron Ajax - 如何在错误事件后访问请求的响应? https://stackoverflow.com/questions/35220177/polymer-iron-ajax-how-to-access-response-from-request-after-error-event?)
The 您链接的问题 https://stackoverflow.com/questions/35220177/polymer-iron-ajax-how-to-access-response-from-request-after-error-event询问关于<iron-ajax>.error
事件,其事件详细信息与<iron-ajax>.response
event.
When <iron-ajax>
收到服务器响应后,激发response event https://github.com/PolymerElements/iron-ajax/blob/6422490/iron-ajax.html#L479与相应的<iron-request> https://elements.polymer-project.org/elements/iron-ajax?active=iron-request作为活动细节。
如果请求因任何原因失败,<iron-ajax>
激发error event https://github.com/PolymerElements/iron-ajax/blob/6422490/iron-ajax.html#L506-L509与object(包含iron-request
通过request
属性,以及潜在的错误error
)作为事件详细信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)