2016-11-23 16 views
1

我有一個使用iron-ajax的自定義元素。我不知道爲什麼請求發生兩次。這是我的代碼:聚合物組件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

聚合物文件說,有關自動設置參數:

「如果屬實,會自動執行一個Ajax請求時,無論是網址或PARAMS變化」

所以我認爲在開始的參數有value =「」,然後從查詢字符串和兩次請求的價值。

我該如何解決這個問題,並且只做一個請求?

謝謝!

回答

3

<iron-ajax>.auto爲true時,如果url是非空字符串,則元素自動生成請求。由於即使idflen爲空白/空,url也具有非空值,因此即使在設置idflen之前,<iron-ajax>也會生成請求。

如果你想<iron-ajax>發送請求,只有當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(); 
    } 
    } 
}); 
+0

完美!謝謝! – Jaime

+0

@Jaime沒問題:) – tony19