JSONP回调失败,需要有关javascript/jquery的帮助 [英] JSONP callback fails, need help with javascript/jquery

查看:85
本文介绍了JSONP回调失败,需要有关javascript/jquery的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是json的菜鸟(知道一些jquery)....并试图使一个小的脚本起作用 我想检索某个纬度/经度的时间 并根据我在网上阅读的内容逐步编写了此脚本:

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:

$.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");
        });
    });

不用说,它是行不通的……有人可以帮我忙一会儿,还可以解释一下什么 $(").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)

这是json源引用: http://www.geonames. org/export/web-services.html#timezone

Here is the json source reference: http://www.geonames.org/export/web-services.html#timezone

谢谢

推荐答案

我最初认为问题很可能出在同源政策中.为了对URL进行AJAX请求,它必须与包含Javascript代码的页面在同一域(和端口)中.

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.

但是在乔治四世纠正之后,我检查了一下.

But after George IV's correction, I checked it out.

回调中返回的data对象是JSON逃避对象,它不是数组.您的代码很可能应该读取如下内容:

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:

$.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)
  }
);

选择器也可能是错误的,例如,您可能希望将结果放入ID为test的div中.在这种情况下,包含选择器的行应更改为:

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:

$("#test").html(data.time);

这是说,获得ID为test的对象(井号(#)表示它是一个主意),并使用设置为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.

这篇关于JSONP回调失败,需要有关javascript/jquery的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆