2017-01-01 99 views
1

我不明白,如何實現在科特林提取API 我的代碼:你如何實現fetch API?

var smf: dynamic = js("({})") 
smf.method = "GET" 
smf.mode = "cors" 
smf.cache = "default" 

window.fetch(url, smf) 
     .then({response -> { 
      console.log("response") 
     }}) 
     .catch({error -> 
      console.error("error") 
     }) 

而且它不會在所有的工作。沒有控制檯消息和任何

回答

3

我的猜測是,這個問題是你的第一拉姆達內:

.then({response -> { 
    console.log("response") 
}}) 

此代碼沒有做任何事情,因爲它相當於:

.then(fun(response: dynamic){ 
    return {console.log("response")} // creates a lambda and returns it for no reason 
}) 

TL; DR修復代碼去除第二對大括號:

.then {response -> console.log("response")} 
+0

是的,我在lambda之前錯過了「運行」,所以有一個解決方案運行{console.log(「response」)} –

+0

是的,那也是。那個'run'是空的代碼 – voddan