【发布时间】:2016-07-23 16:34:43
【问题描述】:
我正在使用 openweather API,查看示例代码后,我有时会看到这样的 API 调用:
function getWeather(callback) {
var weather = 'http://api.openweathermap.org/data/2.5/forecast?lat=51.5072&lon=0.1275&units=metric';
$.ajax({
dataType: "jsonp",
url: weather,
success: callback
});
}
但偶尔我会看到使用这种方法发出的请求:
function gettingJSON(){
document.write("jquery loaded");
$.getJSON("api.openweathermap.org/data/2.5/weather?q=London&APPID=ee6596241130f193adf1ba90e625cc10",function(json){
document.write(json);
}
这些方法之间的主要区别是什么?一种被认为比另一种更好或更有效?
【问题讨论】:
-
有时数据不是 JSON 对象
-
@Bálint 我读了那个,它没有解释应该使用哪个以及何时使用。特别是如果数据是 JSON(例如来自 Openweather API),是否有理由使用 $.ajax 或只是转到 $.getJSON
-
然后看这个:stackoverflow.com/questions/2887209/… 无论如何都是重复的
标签: javascript json ajax openweathermap