有人可以告诉我如何将此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);
});
});
非常感谢, ç
答案 0 :(得分:1)
contentType
用于设置已发送数据的类型。 GET请求不会发送数据,因此您可能正在讨论接收数据的类型,这可以使用dataType
选项进行更改。
替换:
$.getJSON('json/showreel.json', function(data) {
...
});
由:
$.ajax({
type: 'get',
url: 'json/showreel.json',
dataType: 'application/json'
success: function(data) {
...
}
});