繁体   English   中英

AngularJS和$ http获取JSON-数据未定义

[英]AngularJS and $http get JSON - data is not defined

嗨,我正在尝试使用AngularJS $ http get从Web服务检索一些数据。

我有以下代码片段:

在servicesjs中:

     .factory('BoothDesignatedCoordsService', ['$http', function ($http) {

var factory = {};

factory.getBoothDesignatedCoords = function (strBoothName, intFloorPlanID) {


 var sendToWS;
 var boothDesignatedCoords

    var JSONObj = {
        BoothName: strBoothName,
        FloorPlanID: intFloorPlanID
    };
    sendToWS = JSON.stringify(JSONObj)

    var urlBase = 'http://localhost:4951/wsIPS.asmx/fnGetBoothDesignatedCoords?objJSONRequest=' + sendToWS;
        return $http.get(urlBase) 
}
return factory;

}])

在controllerjs中:

 var boothDesignatedCoords = BoothDesignatedCoordsService.getBoothDesignatedCoords(strListShortListedBooth[i], 3).success(function (response, data, status) {
        console.log("successfully send booth name and floor plan id to ws");
        console.log("data " + data + " , status : " + status);
        console.log("data " + data);
        boothDesignatedCoords = data;

   for (var c = 0; c < boothDesignatedCoords.length; c += 2) {

   }

$ http get成功,因为我能够在浏览器控制台日志中打印“成功将展位名称和平面图ID发送到ws”。 当我尝试打印console.log(“ data” + data)时,它为我提供了一些整数数组的值。 那正是我想要的。 但是在控制器中,我试图将数据分配给变量boothDesignatedCoords,该程序未运行for循环。 我是否缺少一些代码?

编辑:我试图跟踪代码(在controller.js中跟踪名为“ data”的变量),并说“ data not defined”

您似乎对$http Promise中可用的方法及其参数感到困惑。 尝试这个

BoothDesignatedCoordsService.getBoothDesignatedCoords(strListShortListedBooth[i], 3)
.then(function(response) {
  var data = response.data
  var status = response.status

  console.log('data', data) // note, no string concatenation
  // and so on...
})

仅供参考, successerror方法已经过时了一段时间,并且从v1.6.0起已删除。 不要使用它们。

我也强烈建议通过params配置对象传递查询参数

var urlBase = 'http://localhost:4951/wsIPS.asmx/fnGetBoothDesignatedCoords'
return $http.get(urlBase, {
  params: { objJSONRequest: sendToWS }
})

这将确保键和值被正确编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM