我在将session_id传递到http.get函数时遇到了问题,你知道我做错了什么吗?
这是我的控制器:
.controller('feedCtrl', function($scope,$rootScope,$ionicHistory,$state,$http) {
$scope.session_id= sessionStorage.getItem('session_id');
if($scope.session_id == null){
$state.go('login');
}
else {
$http.get('https://m.socialnetwk.com/home/app/feed_load.php?id='+ $scope.session_id +).then(function(rest) {
$scope.records = rest.data;
});
}
})
我在代码中看到的错误:
您应该注入sessionStorage
服务。删除URL末尾的+
。
检查示例:链接
除非你解释如何,否则我不知道问题出在哪里https://m.socialnetwk.com/home/app/端点有效,它会产生什么错误,但我怀疑,正如你所说的,你需要通过session_id而不是获取它,你需要使用$http.post而不是$http.GET
我已经在Postman中测试了您的http请求,它运行正常。
https://m.socialnetwk.com/home/app/feed_load.php?id=4235
并返回:
[
{
"firstname": "4235",
"lastname": "Round",
"profile_image": "jpg/55529055162cf0.jpg",
"lastname": "Round",
"iframe": "",
"media_format": "img",
"media_file_format": "jpg",
"media_post_id": "5851875bda5b3",
"media_author_id": "3",
"mediatxt": ""
},
{
"firstname": "4235",
"lastname": "Round",
"profile_image": "jpg/55529055162cf0.jpg",
"lastname": "Round",
"iframe": "",
"media_format": "img",
"media_file_format": "jpg",
"media_post_id": "583c459a745a4",
"media_author_id": "3",
"mediatxt": ""
},
{
"firstname": "4235",
"lastname": "Round",
"profile_image": "jpg/55529055162cf0.jpg",
"lastname": "Round",
"iframe": "",
"media_format": "img",
"media_file_format": "jpg",
"media_post_id": "583c4597778c1",
"media_author_id": "3",
"mediatxt": ""
},
{
}
]
所以你的错误是:
$scope.session_id=sessionStorage.getItem('session_id');
如果您打印$scope.session,您将得到未定义的结果。因此,http请求返回一个空数组。
让我们修复它
注入会话存储。假设您正在使用ngStore,因此需要注入$sessionStorage。
示例:
.controller('feedCtrl', function($scope,$rootScope,$ionicHistory,$state,$http, $sessionStorage )
更改您的代码:
$scope.session_id=sessionStorage.getItem('session_id');
$http.get('https://m.socialnetwk.com/home/app/feed_load.php?id='+$scope.session_id+).then(function(rest){$scope.records=rest.data;});
到:
$scope.session_id=$sessionStorage.getItem('session_id');
并在URL末尾删除+。
$http.get('https://m.socialnetwk.com/home/app/feed_load.php?id='+$scope.session_id).then(function(rest){$scope.records=rest.data;});
正如杰克告诉你的那样。
对不起我的英语。