简体   繁体   English

getJSON 不起作用

[英]getJSON doesn't work

objecten.js对象.js

    var data = [ {
            image: 'gallery/objecten/bear.jpg',
            thumb: 'gallery/objecten/bear.jpg',
            title: 'my first image',
            description: 'Lorem ipsum caption',
            link: 'http://example.com'
        }
        ];

In my main js file I am doing the following:在我的主 js 文件中,我正在执行以下操作:

  $.getJSON("gallery/objecten/objecten.js",function(data) {
                $.each(data, function(key, value) {
                    alert("hello?");
                });
          });

The alert doesn't work, so what's wrong?警报不起作用,那是怎么回事?

Your json is malformed.您的 json 格式不正确。 Despite of the format, you should not have a var declaration.尽管有格式,但您不应该有var声明。 It should be:它应该是:

objecten.js对象.js

[{
    "image":"gallery/objecten/bear.jpg",
    "thumb": "gallery/objecten/bear.jpg",
    "title": "my first image",
    "description": "Lorem ipsum caption",
    "link": "http://domain.com"
}]

Note that strings and property names in json must be enclosed with "注意 json 中的字符串和属性名称必须用"

To see more about the json "format" and its encoding rules see http://json.org要了解有关 json “格式”及其编码规则的更多信息,请参阅http://json.org

Hope this helps.希望这可以帮助。 cheers干杯

object.js does not contain json object.js 不包含 json

[{
        "image": "gallery/objecten/bear.jpg",
        "thumb": "gallery/objecten/bear.jpg",
        "title": "my first image",
        "description": "Lorem ipsum caption",
        "link": "http://domain.com"
}]

Edited - I removed the parentheses that wrapped it, not sure why I put them in I guess I was looking at the example on http://docs.jquery.com/GetJSON which points to a flickr json page that uses parentheses.已编辑 - 我删除了包裹它的括号,不知道为什么我把它们放进Works now.现在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM