저는 PHP 객체와 결합 된 javascript 객체로 내 첫 걸음을 내딛었습니다. 지금까지 모든 것이 잘 작동하지만,이 함수 밖에서 ajax 성공 응답으로 작성된 javascript 객체에 액세스하는 데 어려움을 겪고 있습니다. 마지막 CONSOLE.LOG가 정의되어 있지함수에서 만든 javascript 객체에 액세스
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
:
여기에 내 JS 코드입니다. onSuccess
함수 외부에서 currentSettings
에 액세스하려면 어떻게해야합니까?
감사합니다.
아약스의 비동기성에 대해서는 생각하지 않았습니다. 고마워요! – Sebsemillia
@Sebsemillia : 당신을 가장 환영합니다. "A"는 비동기입니다! (그러나 다시 "X"는 "XML"을 의미하므로 어쨌든 두문자어와 관련이 없습니다.) –
Hehe, 알아요! 하지만 이제 나는 기억할 것이다 ..;) – Sebsemillia