Iron Ajax-如何从响应功能访问响应? [英] Iron Ajax - How to access Response from on-response function?
问题描述
我有这个元素:
<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中看到的响应是:
The response I see in Firebug is:
<p>Hello word</p>
我想访问_handleResponse
函数中的响应,以便将其设置为resultado
div的innerHTML
,但是没有任何作用.
I want to access the response in _handleResponse
function in order to set it as innerHTML
of the resultado
div, but nothing works.
我尝试过:
-
event.detail.innerHTML
-
event.detail.response
-
event.detail.xhr.response
-
event.detail.xhr.responseText
-
event.detail.request.xhr.response
(此路由不存在.在
event.detail.innerHTML
event.detail.response
event.detail.xhr.response
event.detail.xhr.responseText
event.detail.request.xhr.response
(This route doesn't exist. How can it be the solution in Polymer Iron Ajax - How to access Response from Request after Error Event??)
如果我在启用响应功能时调试并观看e.detail.response值:
If I debug and watch e.detail.response value when in on-response function:
在网络"标签中,我可以看到响应(简单的"hello"):
In network tab I can see the response (simple 'hello'):
推荐答案
实际上,响应数据是在<iron-ajax>.response
事件.您的response
字段为null
,因为您已错误配置 <iron-ajax>.handleAs
.将其设置为json
时,Accept-Type
标头设置为application/json
,并且任何响应都将使用 null
响应正文,根据规范.请注意,hello
和<p>Hello</p>
是无效的JSON字符串.
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.
如果要接收纯文本数据,请将<iron-ajax>.handleAs
设置为text
(默认为json
).
If you want to receive plaintext data, set <iron-ajax>.handleAs
to text
(the default is json
).
event.detail.request.xhr.response
(This route doesn't exist. How can it be the solution in Polymer Iron Ajax - How to access Response from Request after Error Event??)
您链接的问题询问<iron-ajax>.error
事件,该事件的事件详细信息与<iron-ajax>.response
事件不同.
The question you linked asks about the <iron-ajax>.error
event, which has a different event detail than the <iron-ajax>.response
event.
当<iron-ajax>
收到服务器响应时,它会使用相应的
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.
这篇关于Iron Ajax-如何从响应功能访问响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!