2017-02-26 112 views
-1

我得到错误说:SyntaxError: Unexpected token ':'. Parse error."越来越拉JSON的错误从URL

我是否做

url = 'http://api.openweathermap.org/v3/uvi/20,77/current.json?appid=c0d8761ca979157a45651a5c7f12a6be'; 
 
function getJSONP(url, success) { 
 

 
    var ud = '_' + +new Date, 
 
     script = document.createElement('script'), 
 
     head = document.getElementsByTagName('head')[0] 
 
       || document.documentElement; 
 

 
    window[ud] = function(data) { 
 
     head.removeChild(script); 
 
     success && success(data); 
 
    }; 
 

 
    script.src = url.replace('callback=?', 'callback=' + ud); 
 
    head.appendChild(script); 
 

 
} 
 

 
getJSONP(url, function(data){ 
 
    console.log(data); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html><body>

,或者如果我这样做:

url = 'http://api.openweathermap.org/v3/uvi/20,77/current.json?appid=c0d8761ca979157a45651a5c7f12a6be'; 
 

 
function CallURL() { 
 
    $.ajax({ 
 
    url: url, 
 
    type: "GET", 
 
    dataType: "jsonp", 
 
    async: false, 
 
    success: function(msg) { 
 
     console.log(msg); 
 
     JsonpCallback(msg); 
 
    }, 
 
    error: function() { 
 
     // ErrorFunction(); 
 
     // break ; 
 
    } 
 
    }); 
 
} 
 

 
function JsonpCallback(json) { 
 
    alert(json); 
 
    //document.getElementById('summary').innerHTML = json.result; 
 
} 
 

 
CallURL();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html> 
 

 
<body> 
 

 
</body> 
 

 
</html>

回答

0

此网址没有返回JSONP,它的回报JSON:只为几个电话

{"time":"2017-02-25T12:00:00Z","location":{"latitude":18.75,"longitude":76.75},"data":10.8} 

OpeanWeather API支持JSONP,像http://api.openweathermap.org/data/2.5/weather?q=London,uk&callback=test&appid=c0d8761ca979157a45651a5c7f12a6be,其他电话只支持JSON,XML或HTML(见https://openweathermap.org/current详细信息)。

所以使用JSON AJAX调用类型:

url = 'http://api.openweathermap.org/v3/uvi/20,77/current.json?appid=c0d8761ca979157a45651a5c7f12a6be'; 
 

 
function CallURL() { 
 
    $.ajax({ 
 
    url: url, 
 
    type: "GET", 
 
    dataType: "json", 
 
    async: false, 
 
    success: function(msg) { 
 
     console.log(msg); 
 
    }, 
 
    error: function() { 
 
     // ErrorFunction(); 
 
     // break ; 
 
    } 
 
    }); 
 
} 
 

 
CallURL();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html> 
 

 
<body> 
 

 
</body> 
 

 
</html>