如何使用JavaScript从URL获取JSON? [英] How to fetch JSON from a URL using JavaScript?

查看:76
本文介绍了如何使用JavaScript从URL获取JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个HTML文件中使用以下代码

I am using following code in one of my HTML files

var queryURL = encodeURI(yahooUrl + loc + appId);
alert(queryURL);

$.getJSON(queryURL, function(data){
    alert('inside getJSON')
    alert(data);
    var items = [];
    $.each(data, function(key, value){
        items.push('<li id="' + key + '">' + value + '</li>');
    });
    $('<ul/>', {
        'class': 'my-new-list',
        html: items.join('')
    }).appendTo('body');
});`

其中 queryURL 是一个大问题,如果我从浏览器的地址栏加载,我会得到一个包含JSON对象的文件。但是下面的代码不起作用,整个JSON对象显示在Firefox的错误控制台上,错误无效标签。我在查询字符串末尾添加了& callback =?,如SO中的一些答案所述。

where queryURL is one big query which if I load from the browser's address bar I get a file containing a JSON object. But the following code is not working, the whole JSON object is being displayed at the error console of Firefox, with the error 'Invalid Label'. I have added &callback=? at the end of the query string as mentioned in few of the answers here at SO.

任何人都可以建议我做错了吗?

Can any one suggest what I am doing wrong ?

编辑:for


queryURL =http://where.yahooapis.com/geocode?location=107,South%20Market,San%20Jose,San%20Fransico,Leusina,USA,&flags= J& appid = dj0yJmk9SUk0NkdORm9qV2FyJmQ9WVdrou1tVnFUVzlVTm5NbWNHbzlORFl4TnpZME5UWXkmcz1jb25zdW1lcnNlY3JldCZ4PWE1& callback =?

queryURL = "http://where.yahooapis.com/geocode?location=107,South%20Market,San%20Jose,San%20Fransico,Leusina,USA,&flags=J&appid=dj0yJmk9SUk0NkdORm9qM2FyJmQ9WVdrOU1tVnFUVzlVTm5NbWNHbzlORFl4TnpZME5UWXkmcz1jb25zdW1lcnNlY3JldCZ4PWE1&callback=?"

我收到以下错误:

Error: invalid label
Source File: http://where.yahooapis.com/geocode?location=107,South%20Market,San%20Jose,San%20Fransico,Leusina,USA,&flags=J&appid=dj0yJmk9SUk0NkdORm9qM2FyJmQ9WVdrOU1tVnFUVzlVTm5NbWNHbzlORFl4TnpZME5UWXkmcz1jb25zdW1lcnNlY3JldCZ4PWE1&callback=jQuery16404719878257064011_1316606312366&_=1316608283354
Line: 1, Column: 1

源代码:

{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":87,"Found":1,"Results":[{"quality":39,"latitude":"37.336849","longitude":"-121.847710","offsetlat":"37.338470","offsetlon":"-121.885788","radius":34800,"name":"","line1":"","line2":"San Jose, CA","line3":"","line4":"United States","house":"","street":"","xstreet":"","unittype":"","unit":"","postal":"","neighborhood":"","city":"San Jose","county":"Santa Clara County","state":"California","country":"United States","countrycode":"US","statecode":"CA","countycode":"","uzip":"","hash":"","woeid":2488042,"woetype":7}]}}


推荐答案

这可能是因为jQuery自动切换到使用JSONP(因为它是跨域请求)和雅虎显然不使用JSONP但定期JSON 。您是否尝试了旧的 $。ajax() dataType:JSON

This may be caused because jQuery automatically switches to using JSONP (because it's a cross-domain-request) and Yahoo apparently doesn't use JSONP but regular JSON. Have you tried good old $.ajax() with dataType:"JSON"?

使用$ .ajax

        $.ajax({
            url: queryURL,
            dataType: "JSON",
            success: function(data){
              alert('inside getJSON')
              alert(data);
              var items = [];
              $.each(data, function(key, value){
                  items.push('<li id="' + key + '">' + value + '</li>');
              });
              $('<ul/>', {
                  'class': 'my-new-list',
                  html: items.join('')
              }).appendTo('body');
            }
        });

让我在这里非常好,因为我有一个可怕的日子: 工作示例

Let me be exceptionally nice here since I'm having a horrible day: Working example

这篇关于如何使用JavaScript从URL获取JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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