jquery - 无法获取 Json

标签 jquery json parsing

我想获取一个 json 以仅在 div 中显示 syn。

就像:--

$.getJSON('http://words.bighugelabs.com/api/2/eba286cdc7f3619674544d80ce94cb1b/stack/json', function(data) {

         //parse the response to display in a div


        });

output format.

<小时/>

来自链接的 JSON 标记:

{
    "noun": {
        "syn": [
            "batch",
            "deal",
            "flock",
            "good deal",
            "great deal",
            "hatful",
            "heap",
            "lot",
            "mass",
            "mess",
            "mickle",
            "mint",
            "muckle",
            "peck",
            "pile",
            "plenty",
            "pot",
            "quite a little",
            "raft",
            "sight",
            "slew",
            "spate",
            "tidy sum",
            "wad",
            "push-down list",
            "push-down stack",
            "smokestack",
            "push-down storage",
            "push-down store",
            "agglomerate",
            "chimney",
            "cumulation",
            "cumulus",
            "large indefinite amount",
            "large indefinite quantity",
            "list",
            "listing",
            "memory device",
            "mound",
            "storage device",
            "whole lot",
            "whole slew"
        ]
    },
    "verb": {
        "syn": [
            "pile",
            "heap",
            "arrange",
            "lade",
            "laden",
            "load",
            "load up",
            "set up"
        ],
        "rel": [
            "heap up",
            "pile up",
            "stack away",
            "stack up"
        ]
    }
}

最佳答案

由于同源策略,JSON 无法加载,只需将 ?callback=? 附加到您的世界即可。更新后的代码,

$.getJSON('http://words.bighugelabs.com/api/2/eba286cdc7f3619674544d80ce94cb1b/stack/json?callback=?', function(data) {
    //parse the response to display in a div
    //you can access the JSON object from the data object.
});

更新:

当您将 ?callback=? 附加到 $.getJSON 中使用的 URL 时,jQuery 会理解这是一个 JSONP 请求,而不是用于获取 JSON 的 XMLHttpRequest。实际上回调参数可以是任何东西,但值必须是 ? 才能触发 JSONP 请求。

发出请求时,jQuery 将参数替换为 ?callback=jQuery15107307685413397849_1299439987443,每次发出请求时该值都是唯一的。

关于jquery - 无法获取 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211378/

相关文章:

javascript - JQuery 可拖动 div

parsing - 如何在 Rust 中迭代 HashSet 并同时更新它?

java - 如何使用 Java HTMLParser 库解析大型 HTML 文件

javascript - JQuery html() 与 innerHTML

javascript - 如何选择表格中的所有输入字段

javascript - 查找等于 json 文件中某个值的元素

json - 编码 JSON 时如何内联字段?

php - Android使用JSON插入mysql

Android Spring Rest 客户端 "exchange"创建类(406 Not Acceptable )

javascript - 如何确保在页面卸载之前始终调用代码