【问题标题】:JSON data fetching through Yahoo! Placefinder通过 Yahoo! 获取 JSON 数据定位器
【发布时间】:2012-06-28 15:18:21
【问题描述】:

我正在尝试从 Placefinder API 检索信息。我的代码从文本框中获取信息,然后单击按钮将其发送到雅虎。

            function codeAddress(){
            var address = document.getElementById("address").value;
            var RequestUrl = "http://where.yahooapis.com/geocode?q="+address+"&flags=J&callback=ws_results&output=json";
            JSONObject = new XMLHttpRequest();
            JSONObject.open( "GET", RequestUrl, false );
            JSONObject.send( null );
            return JSONObject;
            document.getElementByID("jlatitude").innerHTML=JSONObject.latitude;
            alert(document.write("jlatitude"));
        }

Firebug 告诉我数据已返回,但我无法在故障排除弹出窗口中显示我想要的内容。当我请求马里兰州的地理编码时,placefinder 会返回此信息。

{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":10,"Found":2,"Results":[{"quality":49,"latitude":"39.466626","longitude":"-93.709069","offsetlat":"39.466626","offsetlon":"-93.709069","radius":700,"name":"","line1":"","line2":"Roads, MO","line3":"","line4":"United States","house":"","street":"","xstreet":"","unittype":"","unit":"","postal":"","neighborhood":"Roads","city":"Norborne","county":"Carroll County","state":"Missouri","country":"United States","countrycode":"US","statecode":"MO","countycode":"","uzip":"64668","hash":"","woeid":2482523,"woetype":7},{"quality":49,"latitude":"39.080130","longitude":"-82.537394","offsetlat":"39.080130","offsetlon":"-82.537394","radius":700,"name":"","line1":"","line2":"Roads, OH","line3":"","line4":"United States","house":"","street":"","xstreet":"","unittype":"","unit":"","postal":"","neighborhood":"Roads","city":"Wellston","county":"Jackson County","state":"Ohio","country":"United States","countrycode":"US","statecode":"OH","countycode":"","uzip":"45692","hash":"","woeid":2482522,"woetype":7}]}}

我的警告框没有出现,但数据已存储。

谢谢

【问题讨论】:

    标签: javascript json api yahoo


    【解决方案1】:

    我认为您的 JavaScript 代码示例中有一个简单的问题 - 从 YQL 接收数据后,您有 return JSONObject;,这意味着以下行将永远不会执行。

    要进行故障排除和确认,您可以在返回时写入控制台日志,例如:

    function codeAddress(){
        var address = document.getElementById("address").value;
        var RequestUrl = "http://where.yahooapis.com/geocode?q="+address+"&flags=J&callback=ws_results&output=json";
        JSONObject = new XMLHttpRequest();
        JSONObject.open( "GET", RequestUrl, false );
        JSONObject.send( null );
        console.log( JSONObject.responseText );
    }

    【讨论】:

      【解决方案2】:
      $.getJSON(RequestUrl,{},function(result){
        ...
      });
      

      这会正确地从 Yahoo! 检索 JSON 对象!定位器

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        • 2012-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多