Javascript 在本地加载JSON时,以及在使用coffeescript和jquery的服务器上加载JSON时,数据对象不同
我使用以下coffeescript加载jsonJavascript 在本地加载JSON时,以及在使用coffeescript和jquery的服务器上加载JSON时,数据对象不同,javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,我使用以下coffeescript加载json $.ajax jsonPath, success : (data, status, xhr) => console.log("yea "+data) this.currentIndex = 0; this.imagesVO = data.images this.imageManager = new ImageMan
$.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中获取图像数组,它可以工作
this.imageManager=新的imageManager(data.images)
然而,在服务器上测试时,chrome抱怨data.images未定义,即使json加载为ok。有什么想法吗?您需要指定希望返回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并为您解析它,但是更安全的做法是直接指定它。在coffeescript中如何做到这一点?当我添加$.ajaxjsonpath时,数据类型:“json”success:(数据、状态、xhr)=>console.log(“yea”+数据)
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) ->