$ .ajax填充更改objectname作为函数参数传递?

时间:2015-11-03 14:48:15

标签: javascript ajax

我正在尝试将几个jSON文件解析为多个对象。

我的方法如下:

function download_datasave (target_object) {
//  DOWNLOAD CALCULATION BACKUP
    var filename, response
        filename = target_object.name + '.json' ;

    $.ajax
    ({
        type: "POST",
        url: datasave_hostname,
        timeout : 3000 ,
        data: {
            action : "download", target : filename, data : ""
            },
        success: function (data) {
            response = JSON.parse(data) ;
            this[target_object] = response ;    // doesn´t work , results is empty object
            window[target_object] = response ;  // doesn't work , results in empty object (as its the same)
            aufloesung_history = response ; // does work, but how to solve this for more than one target_Object ??
        },
        error : function (data) { console.log(target_object.name + " : Download failed , ServerMessage : " + data); }
    });
    };

请查看成功内容的评论。像“console.log(response)”这样的东西正在aufloesung_history中返回正确的Object。例如。

有什么想法吗?

derdigge

修改

这就是对象的创建方式:

objects = [
"aufloesung", "aufloesung_history",
"grobsortierung", "grobsortierung_history",
"lcreinigung", "lcreinigung_history",
"fraktionierung", "fraktionierung_history",
"feinsortierung", "feinsortierung_history",
"eindickung", "eindickung_history"
];

function create_objects () {
    for (var i = 0; i < objects.length; i++) {
        window[objects[i]] = {};
        window[objects[i]].name = objects[i] ;
    }
};

EDIT2

我评论了代码中的更多控制台日志:

function download_datasave (target_object, target) {
    //  DOWNLOAD CALCULATION BACKUP

    var filename, response ;

    console.log(this[aufloesung_history]) ;     //undefinied

    if (!(arguments[1])) {
        filename = target_object.name + '.json' ;
    } else {
        filename = arguments[1] + '.json' ;
    }

        $.ajax
        ({
            type: "POST",
            url: datasave_hostname,
            timeout : 3000 ,
            data: {
                action : "download", target : filename, data : ""
                },
            success: function (data) {
                    response = JSON.parse(data) ;
                console.log(aufloesung_history) ;       //  empty object
                    this[target_object] = {} ;
                console.log(aufloesung_history) ;       //  empty object
                    this[target_object] = response ;
                console.log(aufloesung_history) ;       //  empty object
                    aufloesung_history = {} ;
                console.log(aufloesung_history) ;       //  empty Object
                    aufloesung_history = response ;
                console.log(aufloesung_history) ;       //  right contents inside object
                console.log(this[aufloesung_history]) ; //  right contents inside object 
                console.log(this[target_object]) ;      //  right contents inside object
            },
            error : function (data) { console.log(target_object.name + " : Download failed , ServerMessage : " + data); }
        });
};

在正确的对象中,它自己看起来像这样的数据: correct object

.json是在正确的对象中使用JSON.stringify(target_object)在上传函数中创建的,它看起来很相似。

这是一个json blob:

{"name":"aufloesung_history","0":{"start":1446043200,"stop":1446063000,"start_h":"28.10.2015, 15:40","stop_h":"28.10.2015, 21:10","duration":19800},"1":{"start":1446153600,"stop":1446157800,"start_h":"29.10.2015, 22:20","stop_h":"29.10.2015, 23:30","duration":4200},"2":{"start":1446170400,"stop":1446173400,"start_h":"30.10.2015, 3:00","stop_h":"30.10.2015, 3:50","duration":3000},"3":{"start":1446229200,"stop":1446267000,"start_h":"30.10.2015, 19:20","stop_h":"31.10.2015, 5:50","duration":37800},"4":{"start":1446270600,"stop":1446363000,"start_h":"31.10.2015, 6:50","stop_h":"01.11.2015, 8:30","duration":92400},"5":{"start":1446366600,"stop":1446409200,"start_h":"01.11.2015, 9:30","stop_h":"01.11.2015, 21:20","duration":42600},"6":{"start":1446415200,"stop":1446421800,"start_h":"01.11.2015, 23:00","stop_h":"02.11.2015, 0:50","duration":6600},"7":{"start":1446422400,"stop":1446435000,"start_h":"02.11.2015, 1:00","stop_h":"02.11.2015, 4:30","duration":12600},"8":{"start":1446436200,"stop":1446450600,"start_h":"02.11.2015, 4:50","stop_h":"02.11.2015, 8:50","duration":14400},"9":{"start":1446452400,"stop":1446456600,"start_h":"02.11.2015, 9:20","stop_h":"02.11.2015, 10:30","duration":4200},"10":{"start":1446457200,"stop":1446464400,"start_h":"02.11.2015, 10:40","stop_h":"02.11.2015, 12:40","duration":7200},"11":{"start":1446473400,"stop":1446481800,"start_h":"02.11.2015, 15:10","stop_h":"02.11.2015, 17:30","duration":8400},"12":{"start":1446488400,"stop":1446496800,"start_h":"02.11.2015, 19:20","stop_h":"02.11.2015, 21:40","duration":8400},"13":{"start":1446498600,"stop":1446513600,"start_h":"02.11.2015, 22:10","stop_h":"03.11.2015, 2:20","duration":15000}}

EDIT_3(解决方案)

我发现了!!这是因为“this”是ajax中的一个Object。 jQuery和我的大脑造成了这个错误。 ajax成功的console.log(this)指向了正确的方向。我使用密钥名称(例如aufloesung.name)来填充新对象。这只是在项目的第一次加载时发生,所以这样就可以了。请参阅代码中的注释。

1 个答案:

答案 0 :(得分:0)

对我来说很不寻常,但我回答了我自己的问题。请参阅ajax函数内的commnet。

function download_datasave (target_object) {
//  DOWNLOAD CALCULATION BACKUP
var filename, versuch = 0 ;

filename = target_object.name + '.json' ;

$.ajax
({
  type: "POST",
  url: datasave_hostname,
  timeout : 3000 ,
  data: {
    action : "download", target : filename, data : ""
    },
  success: function (data) {
      window[target_object.name] = JSON.parse(data) ;   // Works as ecxpected ;)
      window[target_object] = JSON.parse(data) ;   // does not work (dont know why). target_object is here an object with only one key in it (name)
      this[target_object.name] = JSON.parse(data) ;     // does not "work" because this isnt the DOMwindow Object
  },
  error : function (data) {
    console.log("Versuch : " + versuch + " fehlgeschlagen. Versuche es erneut.") ;

  }
});

};