2015-10-04 61 views
1

Im混合聚合物一點點,我試圖創建一個簡單的獲取請求來檢索我的Github信息。我正在檢索錯誤Polymer.httpRequest is not a function。當明確是。不是函數錯誤,聚合物

Polymer({ 
    is: 'custom-element', 
    properties: { 
     name: '' 
    }, 
    httpRequest: function(type, url){ 
     var getRequest = new XMLHttpRequest(); 
     var data = getRequest.responseText; 
     getRequest.open(type, url, true); 
     getRequest.send(); 
     console.log(data); 
     return data; 
    } 
}); 

Polymer.httpRequest('GET', 'https://api.github.com/users/joshspears3'); 

任何想法爲什麼它會返回此錯誤?我在這裏先向您的幫助表示感謝。

+0

你擺脫聚合物錯誤後,您不能從異步方法返回。並且您在進行Ajax調用之前正在讀取responseText。 – epascarello

+0

你不要用Polymer來調用這個方法。你用它引用的元素調用它。 – epascarello

回答

0

聚合物沒有您創建的httpRequest方法。相反,該方法是爲「自定義元素」創建的。

您可以訪問如下所示的新元素的方法:

<link rel="import" href="../polymer/polymer.html"> 

<dom-module id="custom-element"> 
<template></template> 

<script> 
    Polymer({ 
    is: 'custom-element', 
    properties: { 
    name: '' 
    }, 
    ready: function() { 
     this.httpRequest('GET', 'http://stackoverflow.com'); 
    }, 
    httpRequest: function(type, url){ 
     var getRequest = new XMLHttpRequest(); 
     var data = getRequest.responseText; 
     getRequest.open(type, url, true); 
     getRequest.send(); 
     console.log(data); 
     return data; 
    } 
    }); 
</script> 
</dom-module>