배열을 선언 한 다음 여러 값 행을 반환하는 ajax 호출을 작성합니다. 이전에 선언 한 배열에이 행의 ID를 추가하려고하지만 정의되지 않은 값을 계속 반환합니다.아약스 호출에서 배열에 추가 javascript - undefined
var recipeIds = Array();
$.ajax({
url: url,
type: 'POST',
contentType: "application/json",
dataType: 'jsonp',
crossDomain: true,
jsonp: 'jsoncallback',
cache:true,
success: function(data, status){
$.each(data, function(i,item){
recipeIds.push(item.id);
});
},
error: function(){
console.log("Ajax not working - recipies in cat");
}
});
alert("array0 = " + recipeIds[0]);
의견이 있으십니까?
가능한 복제본 [AJAX 함수에서 변수가 반환되지 않음] (http://stackoverflow.com/questions/12475269/variable-doesnt-get-returned-from-ajax-function) –