使用USA Today Census API时标签错误无效

时间:2011-08-28 18:34:10

标签: jquery jsonp

我正在尝试使用jquery和jsonp来使用今日美国人口普查API json服务。这是代码:

var url = "http://api.usatoday.com/open/census/locations?api_key=yourapikeyhere&keyname=FIPS&sumlevid=3&keypat=36069&format=jsonp&callback=?";

        $.ajax({
            url: url,
            dataType: 'jsonp',
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                console.debug(result);
            }
        });

如果我在浏览器中点击了url,这是有效的json:

{"request":{
        "keyname":"FIPS",
        "keypat":"36069",
        "sumlevid":"3",
        "year":"2010"},
    "response":[



    {"Placename":"Ontario","PlacenameFull":"Ontario County, N.Y.","FIPS":"36069","GNIS":"974133","StateAP":"N.Y.","StatePostal":"NY",
              "PctChange":"0.0768977",
              "Pop":"107931","PctHisp":"0.03408700","PctNonHisp":"0.96591300","PctWhite":"0.93650600","PctNonHispWhite":"0.91835500","PctBlack":"0.02253300","PctAmInd":"0.00255700","PctAsian":"0.01043300","PctNatHawOth":"0.00022200","PctTwoOrMore":"0.01666800","PctOther":"0.01108100","USATDiversityIndex":"0.16165100","PopSqMi":"167.60000000","LandSqMi":"644.10000000","WaterSqMi":"18.50000000","TotSqMi":"662.50000000","Lat":"42.8566949000","Long":"-77.3032768000","HousingUnits":"48193","PctVacant":"0.10736000"},



   "End"
   ]}

Firebug会出现“无效标签”错误,并且不会返回结果。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我更新了服务器,以支持JSONP。 (我本来会早点做的,但我刚从假期回来。)

答案 1 :(得分:0)

您使用的API似乎不支持JSONP(响应未包含在函数调用中)。执行脚本时会导致语法错误,因为不允许散列文字({...})作为顶级语句。

确保API支持JSONP,如果确实如此,请确保使用callback参数的正确名称。

如果它不支持JSONP,则必须通过服务器端语言(或代理请求)来调用它。


JSONP通过在函数调用中包装javascript文字来工作,例如:

jsonp_callback({the: "response"});

如果您实现了函数jsonp_callback()并执行此脚本,您的函数将收到响应。

服务器必须明确支持JSONP才能使其正常工作。