簡體   English   中英

IE6和IE7中的JSON編碼問題

[英]JSON encoding problem in IE6 and IE7

有人可以告訴我如何將此getJSON請求更改為.ajax調用,以便我可以設置contentType選項嗎?

$.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'] + '<br />';
            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);
    });
});

非常感謝,C

contentType用於設置發送數據的類型。 GET請求不會發送數據,因此您可能正在談論接收到的數據的類型,這是使用dataType選項更改的。

更換:

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

通過:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM