I'm a noob at json (know a bit of jquery)....and trying to get a tiny script to work I want to retrieve the time at a certain lat/lng and made up this script in bits from what I've read online:
我是json的一个菜鸟(知道一点jquery)....并且试图让一个小小的脚本工作我想要检索某个lat / lng的时间并从我的位置编写这个脚本在线阅读:
$.getJSON("http://ws.geonames.org/timezoneJSON?lat=47.01&lng=10.2&callback=?",
{ 'uID': 1 },
function(data) {
$.each(data, function(i, item) {
$("<span/>").html(item.time).html(".nowtime");
});
});
Needless to say, it doesn't work...could someone give me a hand with it and also explain what $("").html(item.time).html(".nowtime"); means. (I don't understand what the first is)
毋庸置疑,它不起作用......有人可以帮我一把,也解释一下$(“”)。html(item.time).html(“。nowtime”);手段。 (我不明白第一个是什么)
Here is the json source reference: http://www.geonames.org/export/web-services.html#timezone
这是json源参考:http://www.geonames.org/export/web-services.html#timezone
thanks
3 个解决方案
#1
I originally thought the problem is most likely in the same origin policy. In order to do an AJAX request to a URL, it must be in the same domain (and port) as the page containing the Javascript code.
我原本以为这个问题最有可能出现在同一个原始政策中。为了对URL执行AJAX请求,它必须与包含Javascript代码的页面位于同一个域(和端口)中。
But after George IV's correction, I checked it out.
但在乔治四世纠正之后,我检查了一下。
The data
object returned in the callback is the JSON-evaled object, and it is not an array. Most likely, your code should've read something like:
回调中返回的数据对象是JSON-evaled对象,它不是数组。最有可能的是,你的代码应该是这样的:
$.getJSON("http://ws.geonames.org/timezoneJSON?lat=47.01&lng=10.2&callback=?",
{ 'uID': 1 },
function(data) {
$("<span/>").html(data.time); // Or maybe with a different selector (see below)
}
);
The selector is probably also wrong, You might want, for example, to put the result in a div with an id of test
. The line containing the selector in that case should be changed to:
选择器可能也是错误的,例如,您可能希望将结果放在id为test的div中。在这种情况下,包含选择器的行应更改为:
$("#test").html(data.time);
What this is saying is, get the object with id test
(the hash sign (#) indicates it is an idea), and update the content with whatever data.time
is set to.
这就是说,获取具有id测试的对象(哈希符号(#)表示它是一个想法),并使用任何data.time设置为更新内容。
#2
The span code is creating a span element and then setting it's HTML to the returned time value. The code looks wrong, though, as it immediately sets the HTML to ".nowtime" but never actually adds it to the DOM. I'm guessing that the last .html('.nowtime') really should be .addClass('.nowtime') and there should be a .appendTo(???) somewhere in there, but I'm not sure where you should be appending it.
span代码创建一个span元素,然后将其HTML设置为返回的时间值。但是,代码看起来不对,因为它立即将HTML设置为“.nowtime”,但实际上从未将其添加到DOM中。我猜最后的.html('。nowtime')真的应该是.addClass('。nowtime')并且那里应该有一个.appendTo(???),但我不确定你在哪里应该追加它。
EDIT:
Also, I don't think you need the each function. It appears to be iterating over the members of the data object. You want to just have it use the time property directly.
另外,我认为你不需要每个功能。它似乎是在迭代数据对象的成员。你想让它直接使用time属性。
Example (though, you likely want it added to the DOM elsewhere):
示例(但是,您可能希望将其添加到其他位置的DOM):
$(function() {
$.getJSON("http://ws.geonames.org/timezoneJSON?lat=47.01&lng=10.2&callback=?", { 'uID': 1 }, function(data) {
$("<span/>").html(data.time).appendTo('body');
});
});
Note: if your the JSONP call were to return an array of objects then you would need to iterate through them with the each method. I don't know enough about the service to know if there are methods that would return an array of objects. This call doesn't, though.
注意:如果您的JSONP调用是返回一个对象数组,那么您需要使用每个方法迭代它们。我不太了解服务,知道是否有方法可以返回一个对象数组。但是这个电话不会。
#3
You're not very clear on what's wrong. The callback appears to be trying to get the nowtime field and put it in a control on the page. I think this:
你不清楚什么是错的。回调似乎是试图获取nowtime字段并将其放在页面上的控件中。我认为这:
$("").html
Should have a selector in the double quotes to tell it where the result goes. Something like $("#myfield")....
应该在双引号中有一个选择器来告诉它结果的位置。像$(“#myfield”)......
#1
I originally thought the problem is most likely in the same origin policy. In order to do an AJAX request to a URL, it must be in the same domain (and port) as the page containing the Javascript code.
我原本以为这个问题最有可能出现在同一个原始政策中。为了对URL执行AJAX请求,它必须与包含Javascript代码的页面位于同一个域(和端口)中。
But after George IV's correction, I checked it out.
但在乔治四世纠正之后,我检查了一下。
The data
object returned in the callback is the JSON-evaled object, and it is not an array. Most likely, your code should've read something like:
回调中返回的数据对象是JSON-evaled对象,它不是数组。最有可能的是,你的代码应该是这样的:
$.getJSON("http://ws.geonames.org/timezoneJSON?lat=47.01&lng=10.2&callback=?",
{ 'uID': 1 },
function(data) {
$("<span/>").html(data.time); // Or maybe with a different selector (see below)
}
);
The selector is probably also wrong, You might want, for example, to put the result in a div with an id of test
. The line containing the selector in that case should be changed to:
选择器可能也是错误的,例如,您可能希望将结果放在id为test的div中。在这种情况下,包含选择器的行应更改为:
$("#test").html(data.time);
What this is saying is, get the object with id test
(the hash sign (#) indicates it is an idea), and update the content with whatever data.time
is set to.
这就是说,获取具有id测试的对象(哈希符号(#)表示它是一个想法),并使用任何data.time设置为更新内容。
#2
The span code is creating a span element and then setting it's HTML to the returned time value. The code looks wrong, though, as it immediately sets the HTML to ".nowtime" but never actually adds it to the DOM. I'm guessing that the last .html('.nowtime') really should be .addClass('.nowtime') and there should be a .appendTo(???) somewhere in there, but I'm not sure where you should be appending it.
span代码创建一个span元素,然后将其HTML设置为返回的时间值。但是,代码看起来不对,因为它立即将HTML设置为“.nowtime”,但实际上从未将其添加到DOM中。我猜最后的.html('。nowtime')真的应该是.addClass('。nowtime')并且那里应该有一个.appendTo(???),但我不确定你在哪里应该追加它。
EDIT:
Also, I don't think you need the each function. It appears to be iterating over the members of the data object. You want to just have it use the time property directly.
另外,我认为你不需要每个功能。它似乎是在迭代数据对象的成员。你想让它直接使用time属性。
Example (though, you likely want it added to the DOM elsewhere):
示例(但是,您可能希望将其添加到其他位置的DOM):
$(function() {
$.getJSON("http://ws.geonames.org/timezoneJSON?lat=47.01&lng=10.2&callback=?", { 'uID': 1 }, function(data) {
$("<span/>").html(data.time).appendTo('body');
});
});
Note: if your the JSONP call were to return an array of objects then you would need to iterate through them with the each method. I don't know enough about the service to know if there are methods that would return an array of objects. This call doesn't, though.
注意:如果您的JSONP调用是返回一个对象数组,那么您需要使用每个方法迭代它们。我不太了解服务,知道是否有方法可以返回一个对象数组。但是这个电话不会。
#3
You're not very clear on what's wrong. The callback appears to be trying to get the nowtime field and put it in a control on the page. I think this:
你不清楚什么是错的。回调似乎是试图获取nowtime字段并将其放在页面上的控件中。我认为这:
$("").html
Should have a selector in the double quotes to tell it where the result goes. Something like $("#myfield")....
应该在双引号中有一个选择器来告诉它结果的位置。像$(“#myfield”)......