Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在本地加载JSON时,以及在使用coffeescript和jquery的服务器上加载JSON时,数据对象不同_Javascript_Jquery_Coffeescript - Fatal编程技术网

Javascript 在本地加载JSON时,以及在使用coffeescript和jquery的服务器上加载JSON时,数据对象不同

Javascript 在本地加载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

我使用以下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中获取图像数组,它可以工作 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) ->