2017-02-21 49 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");