다음은 webapi 메서드를 호출하는 Jquery의 코드 조각입니다. GetAllEmployees()
메서드를 호출하면 undefined
이라는 데이터가 반환되고 메서드는 Success
이 호출됩니다. 왜 그런가? 난 내가 함수를 호출하면 결과를 원하는jquery 실패한 webapi 호출
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>GetAllCust</title>
@*<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>*@
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
function GetAllEmployees()
{
var Result;
// debugger;
jQuery.support.cors = true;
$.ajax({
url: 'http://localhost:61883/APIService/api/Employee/GetAllEmployees',// service call
type: 'GET',
dataType: 'json',
success: function (data)
{
Result = data;// Not returning the data
$.each(data, function (index, employee)
{
console.log(employee.EmpName);
alert(employee.EmpName);
});
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
return Result;//Not sending the result
}
// Calling the method here
$(document).ready(function()
{
debugger;
var EmpData = GetAllEmployees();
// I see the empdata as undefined
});
//Once i come out from here i see the method console.log diplsaying the data
</script>
</head>
<body>
<div>
</div>
</body>
</html>
기능은 그것이 성공의 함수 끝에서 호출 왜 데이터를 반환 할 수있다라고 일단이처럼 행동 않는 이유를 정말 혼란. 더 많은 계산이 이루어지면 함수의 결과가 실제로 필요합니다. 정말 이에 대한 도움을 주셔서 감사합니다!
미리 감사드립니다.
안녕하세요, 네트워크 탭을 확인한 결과 웹 API에서 반환 된 데이터와 함께 요청을 받으셨습니까? –
예 개발자 도구 – Kumee
을 사용하여 IE에 들어오는 데이터를 볼 수 있으며 반환 된 데이터 유형은 무엇입니까? 당신은 json으로 응답을 설정합니까? 응답은 유효한 json입니까? –