简体   繁体   中英

Ionic / AngularJS passing ID through a http.get

I'm having trouble passing the session_id through to my http.get function, any ideas what I'm doing wrong?

Heres my controller:

 .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; }); } }) 

Errors I see in the code:

You should do to inject sessionStorage service. Delete + at the end of URL.

Check Example : Link

除非您解释' https://m.socialnetwk.com/home/app/ '端点的工作原理以及它产生什么错误,否则我不会完全知道问题所在,但我怀疑您所说的您需要通过session_id而不是获取它,您需要使用$ http.post而不是$ http.get

I have tested your http request in Postman and it's working ok.

https://m.socialnetwk.com/home/app/feed_load.php?id=4235

and returns:

[
{
"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": ""
},
{
}
]

so your error is:

$scope.session_id= sessionStorage.getItem('session_id');

if you print $scope.session you are going to get undefined. So your http request returns a empty array.

Let's fix it

Inject sessionStorage. Let's suppose you are using ngStore, so you need to inject $sessionStorage .

Example:

.controller('feedCtrl', function($scope,$rootScope,$ionicHistory,$state,$http, $sessionStorage )

Change your code:

$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; });

to:

$scope.session_id= $sessionStorage.getItem('session_id');

and delete + at the end of URL.

$http.get(' https://m.socialnetwk.com/home/app/feed_load.php?id= '+ $scope.session_id).then(function(rest) { $scope.records = rest.data; });

As Jackk tell you.

Sorry for my english.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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