gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:03:37 26 4
gpt4 key购买 nike

嗨,我正在尝试使用 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;

}])

在 Controller 中:

 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) 时,它给了我一些整数数组的值。这正是我想要的。但在 Controller 中我尝试将数据分配给变量boothDesignatedCoords,程序不运行for循环。我缺少一些代码吗?

编辑:我尝试跟踪代码(跟踪controllerjs中名为“data”的变量),它说“数据未定义”

最佳答案

您似乎对 $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 方法已 deprecated有一段时间并从 v1.6.0 开始删除。不要使用它们。

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

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

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

关于javascript - AngularJS 和 $http 获取 JSON - 数据未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com