2016-12-02 141 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!祝你有美好的一天 –