고정! 감사! 아래의 "수정 된 코드"를 참조하십시오.jQuery 반환 값 undefined
목표는 대화 상자에서 데이터를 다시 얻는 것입니다. 많은 기사를 보았지만 그 중 어떤 것도 작동하지 못하기 때문에 웹 서비스를 사용하여 대화 상자와 기본 페이지간에 데이터를주고 받기로 결정했습니다.
웹 서비스에서 되돌아 오는 값을 읽는 코드를 제외한 모든 코드가 제자리에 있습니다. 디버거에서 데이터가 다시 전달되는 것을 볼 수 있지만 호출자에게 반환하면 반환 된 데이터는 정의되지 않습니다.
jQuery 함수 getLocal은 AJAX를 호출하고 좋은 데이터를 다시 가져 오지만,이를 호출하는 함수 (verbListShow)로 돌아 오면 반환 값은 "정의되지 않음"입니다.
이것은 주로 jQuery로 작성된 ASP.NET 페이지에서 발생하며 jQuery 대화 상자를 엽니 다.
function getLocal(name) {
$.ajax({
type: "POST",
async: false,
url: "WebServices/FLSAService.asmx/GetLocalVariable",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ name: name }),
success: function (data) {
var rtn = data.d;
return rtn;
}
});
}
위의 코드는 작동하지만, 호출하면 rtn은 정의되지 않습니다.
function verbListShow(dutyNumber) {
$('#dlgDutyList').dialog({
modal: true,
show: "slide",
width: 250,
height: 250,
open: function (event, ui) {
setLocal("DUTYNUMBER", dutyNumber);
},
buttons: {
"Select": function() {
var id = getLocal("VERBID"); // <*** Returns undefined
var verb = getLocal("VERB"); // <*** Returns undefined
$.ajax({
type: "POST",
async: false,
url: "WebServices/FLSAService.asmx/SetDuty",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ dutyNum: dutyNumber, id: id, verb: verb }),
success: function (data) {
data = $.parseJSON(data.d);
if (data.ErrorFound) {
showMessage(data.ErrorMessage, 2, true);
}
else {
log('Set Duty: ' + data.StringReturn + ' (' + data.intReturn + ')');
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("updateDuty: "
+ XMLHttpRequest.responseText);
}
});
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
$('#dlgDutyList').dialog('open');
FIXED CODE :
function getLocal(name) {
var rtn = "";
$.ajax({
type: "POST",
async: false,
url: "WebServices/FLSAService.asmx/GetLocalVariable",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ name: name }),
success: function (data) {
rtn = data.d;
}
});
return rtn;
}
이름을 게시 할 때 웹 서비스는 무엇을 반환합니까? 그것에 필드가 있습니까? 일부 샘플 데이터를 보여줍니다. –
'return rtn;'아약스 성공 콜백에서 리턴 할 수 없습니다. getLocal 함수를 사용하지 않거나 jqXHR 객체를 반환하는 것이 좋습니다. –