2015-01-11 4 views
-1

이 코드를 사용하여 imagesAndName 객체를 만듭니다. 내가 아약스 호출로부터 데이터를 수신 아래 이 라인은 개체자바 스크립트 아약스가 객체에 데이터를 추가합니다.

imagesAndName[0] = new Array("name", "image"); 

에하지만 코드의 데이터를 추가합니다. 이제이 데이터를 개체에 추가하고 싶습니다. 그러나 그것은 효과가 없습니다.

$(document).ready(function() 
{ 
    // create variables 
    var imagesAndName = {}; 
    fillImagesAndNameObject(); 

function fillImagesAndNameObject() 
{ 
    $.ajax({ 
     url  : '../api/displayinformation.php', 
     contentType: 'application/json', 
     type: "POST", 
     dataType: "json", 
     data: 1, 
     success: function (data) 
     { 
      var present = data["teachers"]; 
      $.each(present, function(key, value) { 
       var name = value["name"]; 
       var image = '<img src="'+ value["image"] +'"height="100" width="100"/>'; 
       imagesAndName[key] = new Array(name,image); 
      }); 
     } 
    }); 

} 
}); 

나는 콜백을 만들 수 있지만 그것은 여전히 ​​내가 문제를 해결 한

function fillImagesAndNameObject(imagesAndName, returnData) { 
    var json = JSON.stringify(
     { 
      "method": "getAbsentTeachers", 
      "displayId": displayId 
     }); 

    $.ajax({ 
     url: '../api/displayinformation.php', 
     contentType: 'application/json', 
     type: "POST", 
     dataType: "json", 
     data: json, 
     success: returnData, 
     error: function() { 
      console.log('Something went wrong'); 
     } 
    }); 
} 
// create variables 
var imagesAndName = {}; 


fillImagesAndNameObject(imagesAndName, function(returnValue){ 
    var present = returnValue["teachers"]; 
    $.each(present, function(key, value) { 
     var name = value["name"]; 
     var image = '<img src="'+ value["image"] +'"height="100" width="100"/>'; 
     alert(image); 
     imagesAndName[key] = new Array("test","test2"); 
    }); 
}); 
+2

당신은'data type'을'json'으로 주장하지만 맨손으로 '1'을 게시하고 있습니다. – haim770

+0

왜 그것이 작동하지 않는다고 생각합니까? 나는 당신이하려고하는 것에 대한 성공이나 실패를 조사하려는 어떠한 시도도 볼 수 없다. – Quentin

+0

는 normaly 난 1 게시하지만 그나마 : VAR의 JSON은 JSON.stringify가 = ( 는 { "방법": "getAbsentTeachers" "DisplayID 표시는" 를 DisplayID 표시}); displayInformation.php를 호출 한 후 정보를 받았지만 객체 이미지에 추가 할 수 없습니다 .AndName – Bham

답변

0

작동하지 않습니다. 데이터, OBJ 데이터가 아약스 호출에서이며, OBJ imagesAndName

입니다 : 문제는 내가 2 PARAMS이 내가 객체 함수 fillObjectWithPresentTeachers에서

var imagesAndName  = {}; 

//makes an ajax request with the specific parameters 
function ajaxRequest(file, json, callback, obj) /* <-- Callback, obj (imagesAndname) */ 
{ 
    $.ajax({ 
     url: file, 
     contentType: 'application/json', 
     type: "POST", 
     dataType: "json", 
     data: json, 
     success: function(data) { 
      if (data["result"] === "success") 
      { 
       //the callback with data from ajax and the object (imagesAndName) 
       callback(data, obj); 
      } 
     } 
    }); 
} 

getPresentTeachers(1); 

/** 
* Call to ajaxRequest to get the present teachers belong to 
* to this displayid 
* @param displayid 
*/ 
function getPresentTeachers(displayid) 
{ 
    var json = JSON.stringify(
     { 
      "method": "getAbsentTeachers", 
      "displayId": displayid 
     }); 
    /* <- adding callback method and the object imagesAndName */ 
    ajaxRequest("../api/displayinformation.php", json, fillObjectWithPresentTeachers, imagesAndName); 
} 

/** 
* Fill the object imagesAndName with data received from the ajaxRequest 
* @param data 
*/ 
function fillObjectWithPresentTeachers(data, obj) 
{ 
    var present = data["teachers"]; 
    for(var y = 0; y < present.length; y++) 
    { 
     obj[y] = new Array(present[y]["name"],present[y]["image"]); 
    } 
    fillList(); 
} 

에 대한 콜백 함수와 추가 PARAM를 추가 할 필요가 있었다

이제 fillList() 함수에서 imagesAndName 길이를 호출하여 corrent 정보를 반환하는지 확인할 수 있습니다.

다른 사람들에게도 유용하기를 바랍니다.

관련 문제