0

我使用以下咖啡脚本加载 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 加载正常。有任何想法吗?

4

1 回答 1

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 并为您解析它,但是直接指定它会更安全。

于 2013-01-17T22:16:00.553 回答