开发者

JSON encoding problem in IE6 and IE7

Can someone tell me how to change this getJSON request to a .ajax call so I can set the contentType option?

$.getJSON('json/showreel.json', function(data) {
    //get total number of JSON objects and generate random numder
    var total = data.length;
    var randnum = Math.floor(Math.random()*total)

    //Loop through each of the JSON objects to check if matches random number
    $.each(data, function(entryIndex, entry) {

        if(entryIndex === randnum){
            var info = '<div class="entry" style="color:' + entry['colour'] + '">';
            info += entry['title'] + '<b开发者_如何学编程r />';
            info += '<a href="' + entry['link_url'] + '">' + entry['website'] + '</a>';
            info += '</div>';

            $('#head-contact,#head-contact a').css('color',entry['colour']);

            //create new image object to preload image
            var img = new Image();
            //once image has loaded execute this code
            $(img).load(function () {
                //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
                $(this).hide();
                $('#showreel').removeClass('loading').append(this);
                $(this).fadeIn(3000);
            }).error(function () {
                // notify the user that the image could not be loaded
            }).attr('src', entry['image_src']);
        }
        $('#info').append(info);
    });
});

Many thanks, C


contentType is used to set the type of sent data. GET requests do not send data so you may be talking about the type of received data, this is changed using the dataType option.

Replace:

$.getJSON('json/showreel.json', function(data) {
    ...
});

by:

$.ajax({
    type: 'get',
    url: 'json/showreel.json',
    dataType: 'application/json'
    success: function(data) {
        ...
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜