简体   繁体   English

Ionic / AngularJS通过http.get传递ID

[英]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? 我在将session_id传递到http.get函数时遇到麻烦,有什么想法我做错了吗?

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. 您应该进行注入sessionStorage服务。 Delete + at the end of URL. 删除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. 我已经在Postman中测试了您的http请求,并且工作正常。

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'); $ scope.session_id = sessionStorage.getItem('session_id');

if you print $scope.session you are going to get undefined. 如果打印$ scope.session,则将变得不确定。 So your http request returns a empty array. 因此,您的http请求返回一个空数组。

Let's fix it 修复它

Inject sessionStorage. 注入sessionStorage。 Let's suppose you are using ngStore, so you need to inject $sessionStorage . 假设您正在使用ngStore,那么您需要注入$ sessionStorage

Example: 例:

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

Change your code: 更改您的代码:

$scope.session_id= sessionStorage.getItem('session_id'); $ 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; }); $ 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'); $ 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; }); $ 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. 正如Jackk告诉你的那样。

Sorry for my english. 对不起我的英语不好。

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

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