0

次の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)

ただし、サーバーでテストすると、jsonが正常にロードされていても、chromeはdata.imagesが未定義であると不平を言います。何か案は?

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 に答える