AJAX 호출에서받은 JSON 데이터를 기반으로 채워지는 JsTree가 있습니다. 다음은 AJAX 호출입니다.jQuery의 .ajax 함수에서 "success"를 반환해야합니다.
function sendQuery(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: function(data) {
// ^^^^ Need for sendQuery() to return DATA
},
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
}
여기에는 범위 문제가 있음을 알고 있습니다. 자바 스크립트 변수는 선언 함수에 따라 정의됩니다. 나는 sendQuery()에서 리턴하는 방법을 모른다. JSON을 파싱 할 다른 함수의 매개 변수로 사용하고있다. JSON은 다른 트리를위한 매개 변수이다. 내가 자바에서 익숙한 것만큼이나 간단하지 않은 시계에서이 부분에 좌절감을 느꼈다. 도움을 많이 주셔서 감사 드리며 작동하는 경우 분명히 받아들입니다. 건배
EDIT # 1 : 그래, 대답에 따라, 나는이 방식으로 내 코드를 변경하면 데이터가 .ajax 함수에서 벗어날 수 있다고 믿습니다. 프로그램의 흐름으로 되돌려 놓는 방법에 대한 질문이 남아 있습니다.
function sendQuery(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: getJson,
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
}
function getJson(data){
alert("Transmission Success.");
alert(data);
var obj = $.parseJSON(data);
alert("Parsing JSON Success.");
var apples = obj.apples;
alert(apples);
return apples;
}
이제 어떻게하면 트리의 데이터를 준비 할 호출 체인에 APPLES 변수를 가져올 수 있습니까?
데이터를 처리 할 함수에 APPLES 변수를 제공해야합니다.
편집 # 2 콜백을 사용하여 :
콜백의 아이디어 주위에 내 머리를 정리 나에게 두 번째했다. 여기 내가 그걸로 할 수 있었던 것이있다.내 원래의 트리 코드는 여러 가지 기능을 수행하는 일련의 함수를 호출하지만 궁극적으로 트리가 허용 할 형식으로 데이터를 가져옵니다.
$(function() {
$("#client_tree").jstree({
"json_data": {"data": attachTree(stageTreeData(getJson(sendQuery())))},
"plugins" : [ "themes", "json_data", "ui" ]
}).bind("select_node.jstree", function (e, data) {
var msg = data.rslt.obj.attr("id");
alert(msg);
});
});
나는 순간
지금은 전화를 해달라고, 내가 약간 변경] ... 방법은 다음 데이터 등로에서 반환()을 sendQuery에서, Ajax를 통해 데이터를 얻기 위해 노력하고 있어요 sendQuery(), jQuery가 호출합니다.
$(function(){
$.ajax({
context: this,
url: 'http://localhost:8080/testMain',
type: 'GET',
dataType: 'text',
success: loadTree,
error: function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
});
});
또한 나는 오류, 예외없이이없고 나무가 채워집니다 내 나무 로딩 코드를 조금 ...
function loadTree(data){
$("#client_tree").jstree({
"json_data": {"data": attachTree(stageTreeData(getJson(data)))},
"plugins" : [ "themes", "json_data", "ui" ]
}).bind("select_node.jstree", function (e, data) {
var msg = data.rslt.obj.attr("id");
alert(msg);
});
}
을 변경했습니다.
도움 주셔서 감사합니다.
편집 # 3 약간의 물건을 고정 :
이전 Alert() call in jQuery not displaying, called from within a JsTree
오, 그렇다면 성공과 같은 함수를 전달하면 : functionName은 함수에 데이터를 다시 전달합니다. 그게 최고야. 정말 고마워. 아프다. –
yes ..'success : functionName'은'success : function (data) {functionName (data)} '와 똑같이 동작 할 것입니다. – charlietfl
방금 질문을 수정했습니다.이 데이터를 함수 호출에 보내야합니다. 잠시 시간을내어 확인해주세요. 감사합니다 –