【发布时间】:2014-02-06 15:42:35
【问题描述】:
我正在使用 javascript 对象与 php 对象相结合来迈出第一步。到目前为止一切正常,但我很难在此函数之外访问在 ajax 成功响应中创建的 javascript 对象。
这是我的 JS 代码:
function settings(mannstage, stundenlohn, tags) {
this.mannstage = mannstage;
this.stundenlohn = stundenlohn;
this.tags = tags;
}
var currentSettings;
SendAjaxJsonRequest("getSettings");
function SendAjaxJsonRequest(method, jsonObject) {
jsonObject = (typeof jsonObject === "undefined") ? "none" : jsonObject;
$.ajax({
type: "POST",
url: "app/class/controller/ajax.php",
data: {
method: method,
jsonObject: jsonObject
},
success: onSuccess
})
};
function onSuccess(content) {
var response = $.parseJSON(content);
currentSettings = new settings(response.mannstage, response.stundenlohn, response.tags);
console.log(currentSettings); //Returns the object
}
console.log(currentSettings); //This is undefined
最后一个 console.log 未定义。如何在onSuccess 函数之外使currentSettings 可访问?
谢谢!
【问题讨论】:
标签: javascript ajax variables object