javascript - 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});
};
解决方案
这是工作吗?
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;
};
推荐阅读
- python-3.x - 由于 cron 作业导致的高 CPU 使用率问题
- apache-kafka - 在 Kafka-Connect 中自动重新连接失败的任务
- javascript - jQuery悬停不允许光标进入下拉菜单
- typescript - 比较两个具有不同精度打字稿火力的数字
- java - 没有版本声明的 Gradle 依赖项无法解析
- python-3.x - 发送 Python 3.x API POST 请求而不杀死网络服务器
- node.js - 测试 hapi 路由时根据要求设置参数
- python - 如何下载具有长 URL 长度的图像?
- python-3.x - Discord 机器人权限配置
- c# - 使用 SignalR 从实时 SQL 更新数据时出现问题