2017-02-21 30 views
-2

在調用Open Weather API時,控制檯中出現意外的令牌錯誤。意外的令牌','api call ajax

function ajaxCall() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.open("POST", api.openweathermap.org/data/2.5/weather?zip=94040,us); 
    xhttp.setRequestHeader("Content-type", "application/json"); 
    xhttp.send(); 
    var response = JSON.parse(xhttp.responseText); 
    console.log(response); 
} 

錯誤意外標記是郵政編碼和國家之間的逗號。

+0

我猜應該是'+' –

+2

的URL缺少一些報價 - >'「api.openweathermap.org/data/2.5/weather?zip= 94040,我們「' – Andreas

+0

這不是一個相對的URL。它需要從一個方案開始(比如'http://') – Quentin

回答

0

您的網址缺少引號。變化:

xhttp.open("POST", api.openweathermap.org/data/2.5/weather?zip=94040,us); 

到:

xhttp.open("POST", "api.openweathermap.org/data/2.5/weather?zip=94040,us");