首页 > 解决方案 > Javascript 和 Jquery - 在 $.get running 中获取类定义

问题描述

我没有得到跟随运行。有一个设备类。在这个类中,我必须通过 $.get 请求获取一些变量。该请求将从 .php 脚本中获取数据,该脚本从 MySQL 数据库中收集数据。

错误是“this.device 不是构造函数”。认为它在函数范围内找不到。有人可以给我一个提示吗?通常我在 C# 中,对不起:)

帮助?

var link150 = function() {  

    //Array zum Sammeln der Devices
    this.devices = []; 

    //#####################
    //Definition eines Devices
    //#####################

    function general_config() {
        this.database_id = "";
        this.devicename = "";
        this.hardware_id = "";
        this.serialnumber = "";
        this.ip = "";
        this.firmware_version = "";
        this.model = "";
        this.password = "";
    }

    this.device = function() {
        this.database_id = "";
        this.devicename = "";
        this.username = "";
        this.hardware_id = "";
        this.fqdn = "";
        this.kunde_id = "";
        this.location = "";
        this.ip = "";
        this.defaultPW = "";
        this.generalConfig = new general_config(); //TEST
        this.generalConfig.model ="EGX150";  //TEST
    }
    jQuery.ajaxSetup({async:false});

    //#####################
    //Devices aus Datenbank laden
    //#####################
    this.loadDevices = function() {
        _devices = [];
        $.get('php/abfrage.php', {action:'loadDeviceList'}, function(data){
            $.each(JSON.parse(data), function(i, item) {
                tmp = new this.device();
                tmp = {
                    id: item.id,
                    hardware_id: item.hd_id,
                    devicename: item.devicename,
                    fqdn: item.fqdn,
                    kunde_id: item.kunde_id,
                    location: item.kunde_location,
                    ip: item.ip,
                    lastsid: item.lastSID
                    };
                _devices.push(tmp);
            });
        })
        .always(function(){
            hide_loader();
        });
        this.devices = _devices;
    };

    //#####################
    //Geräte laden
    //#####################
    this.loadDevices();

    jQuery.ajaxSetup({async:true}); 
};

标签: javascriptjqueryclassoop

解决方案


这是工作吗?

var _self=this;
 this.loadDevices = function() {
        _devices = [];
        $.get('php/abfrage.php', {action:'loadDeviceList'}, function(data){
            $.each(JSON.parse(data), function(i, item) {
                tmp = new _self.device();
                tmp = {
                    id: item.id,
                    hardware_id: item.hd_id,
                    devicename: item.devicename,
                    fqdn: item.fqdn,
                    kunde_id: item.kunde_id,
                    location: item.kunde_location,
                    ip: item.ip,
                    lastsid: item.lastSID
                    };
                _devices.push(tmp);
            });
        })
        .always(function(){
            hide_loader();
        });
        this.devices = _devices;
    };

推荐阅读