作为函数参数传递的$.ajax-fill更改对象名

$.ajax fill changing objectname passed as function argument?

本文关键字:对象 ajax-fill 函数 参数传递      更新时间:2023-09-26

我正在尝试将几个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。

有什么想法吗?

挖掘

编辑

这就是创建对象的方式:

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); }
        });
};

数据本身在正确的对象中看起来是这样的:正确的目标

.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)来填充一个新对象。这只是在项目的第一次加载时发生的,所以这是可以的。请查看代码中的注释。

这对我来说很不寻常,但我回答了自己的问题。请参阅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.") ;
  }
});

};