我使用以下coffeescript加载json
$.ajax jsonPath,
success : (data, status, xhr) =>
console.log("yea "+data)
this.currentIndex = 0;
this.imagesVO = data.images
this.imageManager = new ImageManager(data.images)
this.imagesCount = this.imagesVO.length
this.switchToImage(this.currentIndex)
error : (xhr, status, err) ->
$('#imageHolder').html("problem loading the json file, </br>make sure you are running this on your local server")
complete : (xhr, status) ->
#cconsole.log("comp")
json就是这样
{
"showName": "aaa",
"galleryName": "Season 3 Preview",
"images": [
{
"title": "Les goes shopping for a new car",
"url": "images/hcp_stills-0.jpeg",
"description": "Sailboats on the Charles River"
},
{
"title": "Les goes shopping for a new car",
"url": "images/hcp_stills-1.jpeg",
"description": "Sailboats on the Charles River"
},
{
"title": "Les goes shopping for a new car",
"url": "images/hcp_stills-2.jpeg",
"description": "Sailboats on the Charles River"
}]
}
在本地测试时,我会像这样从json中获取图像数组,并且它可以工作 this.imageManager = new ImageManager(data.images)
然而,当在服务器上进行测试时,chrome会抱怨data.images未定义,即使json已经加载了ok。任何想法?
答案 0 :(得分:0)
你需要指定你期待json回来。
success : (data, status, xhr) =>
console.log("yea "+data)
this.currentIndex = 0;
this.imagesVO = data.images
this.imageManager = new ImageManager(data.images)
this.imagesCount = this.imagesVO.length
this.switchToImage(this.currentIndex)
dataType: "json"
error : (xhr, status, err) ->
如果服务器使用正确的标头响应,它可能会检测到它是json并为您解析它,但是继续并指定它会更安全。