2016-12-02 1 views
0

함수가 응답을 얻기 전까지는 정상적으로 실행되는 것으로 보이는 PHP 파일에 값을 게시합니다. PHP 파일은 한 행의 JSON 응답을 반환합니다.변수에 POST json 값을 가져올 수 없습니다.

하지만 변수로 설정할 수 없으며 정상적인 조건에서 작동하는 것으로 보이는 variablname [0]으로 사용할 수 없습니다.

여기 내 코드입니다 :

expoList.showdetail = function(expoID) { 
      var detay; 
      /*$scope.content = 'Please click the icons on the map for exposition event details. expoID=' + expoID;*/ 
      $http({ 
       method: 'POST', 
       url: base_url + '/Exposite_info_controller/get_expo_data', 
       data: "expoID=" + expoID, 
       headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'} 
      }).then(function(response) { 
       $scope.expoList.detay = response.data; 
      }); 
      var writehtml = ''; 
      writehtml += '<div id="info-image"><img src="images/expositions/' + detay[0].imagename + '" width="300" height="188" /></div>\r\n'; 
      writehtml += '<div id="address-info">' 
      writehtml += '<strong>' + detay[0].expoName + '</strong><br />\r\n' 
      writehtml += detay[0].expoAddress + '<br />\r\n'; 
      if(detay[0].expoAddress2 != '') { 
       writehtml += detay[0].expoAddress2 + '<br />\r\n'; 
      } 
      writehtml += detay[0].expoCity + '<br />\r\n'; 
      writehtml += '</div>\r\n'; 
      $scope.content = writehtml; 
    }; 

그리고 이것은 PHP 파일에서 반환 것입니다 :

[{"expoID":"2","expoName":"CONGRESIUM ANKARA","expoAddress":"Sogutozu Caddesi No:1\/A, 06510 Cankaya","expoAddress2":"","expoCity":"Ankara","top":"150","left":"355","imagename":"congresiumankara.jpg","startdate":"2016-12-02","enddate":"2016-12-03"}] 

내가 콘솔에서 다음과 같은 오류 메시지가 얻을 :

angular.min.js:122 TypeError: Cannot read property '0' of undefined 
    at Object.expoList.showdetail (main.js:21) 
    at fn (eval at compile (angular.min.js:236), <anonymous>:4:300) 
    at e (angular.min.js:281) 
    at b.$eval (angular.min.js:147) 
    at b.$apply (angular.min.js:147) 
    at HTMLAnchorElement.<anonymous> (angular.min.js:281) 
    at bg (angular.min.js:38) 
    at HTMLAnchorElement.d (angular.min.js:38)(anonymous function) @ angular.min.js:122(anonymous function) @ angular.min.js:94$apply @ angular.min.js:147(anonymous function) @ angular.min.js:281bg @ angular.min.js:38d @ angular.min.js:38 
+0

같은 다음 콜백 기능의 리스폰스에 따라 행동하기로 코드를 넣어 반환 응답 데이터와 함께 typeof **? 콜백 함수에서'typeof response'를 시도하십시오. – Anson

+0

변수'detay'를 채우는 것을 보지 못했습니다 – Beginner

답변

1

먼저, detay이 정의되어 있지 않으므로이 오류가 발생합니다. 대신 응답을 detay = response.data;으로 설정하십시오.

둘째는이 $ HTTP는 ** 약속은 그래서 당신은 당신이 사용하는 경우 결과가 무엇인지 말해 줄 수 안쪽이

expoList.showdetail = function(expoID) { 
      var detay; 
      /*$scope.content = 'Please click the icons on the map for exposition event details. expoID=' + expoID;*/ 
      $http({ 
       method: 'POST', 
       url: base_url + '/Exposite_info_controller/get_expo_data', 
       data: "expoID=" + expoID, 
       headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'} 
      }).then(function(response) { 
       detay = response.data; 

       //Add code here 

        var writehtml = ''; 
        writehtml += '<div id="info-image"><img src="images/expositions/' + detay[0].imagename + '" width="300" height="188" /></div>\r\n'; 
        writehtml += '<div id="address-info">' 
        writehtml += '<strong>' + detay[0].expoName + '</strong><br />\r\n' 
        writehtml += detay[0].expoAddress + '<br />\r\n'; 
        if(detay[0].expoAddress2 != '') { 
         writehtml += detay[0].expoAddress2 + '<br />\r\n'; 
        } 
        writehtml += detay[0].expoCity + '<br />\r\n'; 
        writehtml += '</div>\r\n'; 
        $scope.content = writehtml; 
      }); 

    }; 
+0

감사합니다. –

+0

Np! 좋은 하루 되세요 –

관련 문제