【问题标题】:Polymer component iron-ajax make two requests聚合物组件 Iron-ajax 提出两个要求
【发布时间】:2016-11-23 11:36:52
【问题描述】:

我有一个使用 Iron-ajax 的自定义元素。我不知道为什么请求发生两次。这是我的代码:

<template>

    <div style="text-align: center" hidden="{{!cargando}}">cargando ... <br />
        <paper-spinner alt="cargando ..." active="[[cargando]]"></paper-spinner>
    </div>

    <ficha-folleto datos="[[ajaxResponse]]"></ficha-folleto>

    <iron-ajax 
        auto 
        url="backend/api.php?operacion=folleto&idf=[[idf]]&len=[[len]]" 
        handle-as="json" 
        verbose=true 
        last-response={{ajaxResponse}} 
        loading="{{cargando}}"> </iron-ajax>
</template>

<script>
    Polymer({
        is: "folleto-digital",
        properties: {
        }
    });
</script>

电话来自这个页面:

<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="elements/folleto-digital/folleto-digital.html">
<!DOCTYPE html>
<html>
    <head>
        <script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <folleto-digital idf="" id="folleto"></folleto-digital>
    <script src="js/funciones.js"></script>
    <script>
        var idf = getParameterByName("idf");
        var folleto = document.querySelector("#folleto");
        folleto.idf = idf;

        var len = getParameterByName("len");
        folleto.len = len;
    </script>
</body>
</html>

我请求这个网址:folleto.html?idf=1&len=es

一切正常,但有两个请求:

  1. api.php?operacion=folleto&idf=&len=
  2. api.php?operacion=folleto&idf=1&len=es

Polymer 文档中提到了自动参数:

"如果为 true,则在 url 或 params 更改时自动执行 Ajax 请求"

所以我认为开头的参数有 value="" 然后从查询字符串中获取值,并且由于该请求两次。

我怎样才能解决这个问题,只做一个请求?

谢谢!

【问题讨论】:

    标签: polymer


    【解决方案1】:

    &lt;iron-ajax&gt;.auto 为真时,如果url 是非空字符串,则元素会自动生成请求。由于 url 具有非空值,即使 idflen 为空白/空,&lt;iron-ajax&gt; 甚至在您设置 idflen 之前就会生成请求。

    如果您希望 &lt;iron-ajax&gt; 仅在设置了 idflen 时发送请求,则需要删除 auto,并在 idflen 上添加一个复杂的观察者,它会生成仅当两个值都不为空时才请求。

    // template
    <iron-ajax id="ajax" ...>
    
    Polymer({
      is: "folleto-digital",
    
      properties: {
        idf: String,
        len: String
      },
    
      observers: ['_sendRequest(idf, len)'],
    
      _sendRequest: function(idf, len) {
        if (idf && len) {
          this.$.ajax.generateRequest();
        }
      }
    });
    

    【讨论】:

    • 完美!!谢谢!
    • @Jaime 没问题 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2015-08-18
    相关资源
    最近更新 更多