Iron Ajax-如何从响应功能访问响应? [英] Iron Ajax - How to access Response from on-response function?

查看:115
本文介绍了Iron Ajax-如何从响应功能访问响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个元素:

<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'):

推荐答案

实际上,响应数据是在event.detail.response中返回的. -ajax#event-response"rel =" noreferrer> <iron-ajax>.response 事件.您的response字段为null,因为您已错误配置 <iron-ajax>.handleAs .将其设置为json时,Accept-Type标头设置为application/json,并且任何响应都将使用规范.请注意,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).

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

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

  • event.detail.request.xhr.response(此路由不存在.在
  • 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>收到服务器响应时,它会使用相应的 response事件 > 作为事件详细信息.

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

如果请求由于任何原因失败,<iron-ajax>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆