如何使 jQuery JSON 返回的数据在全球范围内可用?小字符串解析有帮助吗?
我在 $.getJSON
的帮助下进行了 API 调用,但我不知道如何使返回的数据在全局范围内可用,以便在其他函数中使用。
下面代码中的return
不起作用,但如果我alert
它:它会起作用。
$.getJSON(url, function(timeInfo){
return timeInfo.time;
});
也出了2010-12-09 12:53的时间格式。我如何取出 12(小时)。
请帮忙,很抱歉将两个问题合二为一。
详细代码:
function handle_geolocation_query(position) {
if(position[0] == 'Yes'){
var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
} else {
var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
}
$.getJSON(url, function(weatherInfo){
var clouds = weatherInfo.weatherObservation.clouds;
var weather = weatherInfo.weatherObservation.weatherCondition;
var time = getTimeInfo(position);
if(time.getHours()>=6 && time.getHours()<=18){
var weatherClass = placeImageDay(clouds, weather);
$('#weatherImage').show().addClass(weatherClass);
} else {
var weatherClass = placeImageNight(clouds, weather);
$('#weatherImage').show().addClass(weatherClass);
}
});
}
function getTimeInfo(position){
if(position[0] == 'Yes'){
var url = "http://ws.geonames.org/timezoneJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
} else {
var url = "http://ws.geonames.org/timezoneJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
}
$.getJSON(url, function(timeInfo){
return timeInfo.time;
});
}
I have made an API call with the help of $.getJSON
but I don't know how do I make the returned data available globally to be used in other functions as well.
The return
in below code doesn't work but instead if I alert
it: it works charms.
$.getJSON(url, function(timeInfo){
return timeInfo.time;
});
Also out of 2010-12-09 12:53 time format. How do I take the 12 (the hour) out.
Please help and sorry for including two questions in one.
Detailed code:
function handle_geolocation_query(position) {
if(position[0] == 'Yes'){
var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
} else {
var url = "http://ws.geonames.org/findNearByWeatherJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
}
$.getJSON(url, function(weatherInfo){
var clouds = weatherInfo.weatherObservation.clouds;
var weather = weatherInfo.weatherObservation.weatherCondition;
var time = getTimeInfo(position);
if(time.getHours()>=6 && time.getHours()<=18){
var weatherClass = placeImageDay(clouds, weather);
$('#weatherImage').show().addClass(weatherClass);
} else {
var weatherClass = placeImageNight(clouds, weather);
$('#weatherImage').show().addClass(weatherClass);
}
});
}
function getTimeInfo(position){
if(position[0] == 'Yes'){
var url = "http://ws.geonames.org/timezoneJSON?lat=" + position[1] + "&lng=" + position[2] + "&callback=?";
} else {
var url = "http://ws.geonames.org/timezoneJSON?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";
}
$.getJSON(url, function(timeInfo){
return timeInfo.time;
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从
$.getJSON()
中的成功回调函数将数据传递给其他函数和对象。这样做的方法是创建一个全局对象或函数:编辑:添加了将全局函数作为第二个参数传递的示例
您还可以将全局函数作为第二个参数传递:
这样,
globalFunction
就会传递响应中返回的任何内容。编辑 2:添加了时间提取的正则表达式示例
要提取日期的小时部分,您可以使用正则表达式来匹配组件:
有关详细信息,请参阅http://en.wikipedia.org/wiki/Regular_expression
You can pass the data to other functions and objects from the success callback function in
$.getJSON()
. The way you do that is either create a global object or function:EDIT: Added example of passing global functions as second parameter
You can also pass a global function as second parameter:
That way,
globalFunction
gets passed whatever is returned in the response.EDIT 2: Added regular expression example for time extraction
To extract the hour portion of the date, you can use a Regex to match the components:
For more info, see http://en.wikipedia.org/wiki/Regular_expression
这是异步调用,您不能返回类似的值,它只是实例化进程并继续,您希望在返回处理程序中执行的任何操作
This is asyncronous call and you cannot return a value like , it just instantiate the proces sand goes on , anything you want you have do inside return handler