Sending ID via http.get request in Ionic using AngularJS

Can anyone help me figure out why I can't pass the session_id to my http.get function?

This is what my controller looks like:

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

Answer №1

Issues spotted in the code:

Ensure to incorporate the sessionStorage service. Remove the unnecessary + at the end of the URL.

Refer to this Example: Link

Answer №2

Without a clear understanding of how the '' endpoint functions and the specific error it generates, pinpointing the exact issue is challenging. However, based on your statement about passing the session_id instead of getting it, my suspicion is that utilizing $http.post rather than $http.get might be the solution.

Answer №3

I have successfully tested the http request in Postman and it seems to be functioning properly.

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

This request returns the following:

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

The issue appears to be related to this line of code:

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

Upon printing $scope.session, it returns as undefined, causing the http request to return an empty array.

Let's resolve this issue

Make sure to inject sessionStorage. If you are using ngStore, you need to inject $sessionStorage.

Here is an example:

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

Update your code snippet as follows:

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

$http.get('' + $scope.session_id).then(function(response) { $scope.records = response.data; });

As mentioned by Jackk.

Apologies for any errors in my English.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the Element from the Shadow DOM or leverage the main site's styles

I am currently involved in a project that utilizes Polymer (still on version 1.7, but we are planning to switch to a different technology instead of upgrading). My task involves exploring ways to integrate DynamicYield, a personalization platform that inse ...

Switching between multiple views to a single view using Angular UI-router

I am working on an application that consists of three views (ui-view using Angular ui-router): header, sidebar, and content. Here is a simplified version of my index.html: <body> <div ui-view="header" class="..."></div> <div ...

Capture each touchdown and convert it to currency format

As a novice in the field of JS, I have put in a lot of effort into learning from various sources such as websites, documentation, friends, and other questions on Stack Overflow. However, despite all my efforts, I seem to be stuck at this point. $(docume ...

An irritating problem with TypeScript-ESLint: when a Promise is returned without being resolved

Check out this TypeScript snippet I've simplified to showcase a problem: import * as argon2 from "argon2"; export default async function(password:string):Promise<string> { return argon2.hash(password, { type: argon2.argon2id, ...

The Three.js effect is functional on Firefox but experiencing issues on Chrome

I have recently implemented a recursive search for adjacent faces using a Half Edge data structure and I am facing an issue where the effect is visible on Firefox but not on Chrome. I am seeking assistance in resolving this problem. Any help or suggestions ...

Passport.js does not provide authentication for server-side asynchronous requests

Recently, I configured Passport.js with the local-strategy on my express server. Interestingly, when I am logged in and send an asynchronous request within NextJS's getInitialProps, it allows the GET request through client-side rendering but not serv ...

How to Transfer Data from SuperAgent Library Outside the .then() Block?

I have a dilemma in my Nodejs project with two interdependent files. The key to this issue lies in the usage of a crucial library known as SuperAgent (I need it) Check out SuperAgent Library Here In file1.js const file2 = require('./file2'); ...

Storing the initial toggle state in localStorage with React hooks is a helpful way to save user

I'm currently working on persisting the state of a toggle in local storage, as an example of maintaining a modal or a similar element even after a page refresh. Most of my implementation is functional - upon the initial page load, the boolean value i ...

Issues detected between Angular and Express rendering operations

Having an issue with my angular code. It runs perfectly on its own, but when I try to access it on localhost with express, it only displays the HTML file. Here's my server code: var express = require('express'), app = express(); app ...

Using a function as an argument within an Angular directive

Looking for a solution to pass a promise-returning function into a directive? Here's what I'm currently doing: In the parent controller, I've created a callback: $scope.myCb = function(data) { console.log(data); } Directive Scope: sco ...

What is the reason for Vue regular slots being accessible in this.$scopedSlots?

Please have a look at this simple example Testing.vue <template> <div> <slot name="this_is_not_scoped_slots"/> </div> </template> <script> import Vue from "vue"; export default Vue.extend({ ...

Interactive questioning system using Javascript/jQuery for Quick Responses

Hi there! I'm new to StackOverflow and a bit of a beginner when it comes to javascript/jquery. My current project involves creating a chat interface that looks like SMS text messages. Right now, I have users inputting text and using javascript to disp ...

Change the background color of the MUI ToggleButton that is currently selected

I need help figuring out how to change the background color of a selected toggle button. Currently, the buttons are functional but do not visually indicate when one is selected. I would like the first button (Btn 1) to have a default color, and if the us ...

Creating sitemaps on Next.js using next-sitemap is not supported

Creating dynamic sitemaps for my Next.js site using next-sitemap has hit a snag. Following the documentation, I've implemented the following code: //pages/server-sitemap-index.xml/index.js import axios from "axios"; import { getServerSideSi ...

Encountering an issue with finding the module `scheduler/tracing` in React Native

Encountering an error during the react-native run-android process: Error: Unable to resolve module `scheduler/tracing` from `/Users/miftahali/projects/react/appscustomec/node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js`: Module ...

"The PHP code was unable to process the Ajax response and encountered

I am having trouble retrieving a response from an ajax call. Despite my best efforts, I can't seem to access the response. Below is the JavaScript code I am using: $.ajax({ type: "POST", url: "inc.test.php", data: {list:postData}, d ...

Prop validation error: prop type mismatch occurred

My Vue.js countdown isn't displaying the values correctly. Despite defining everything as numbers, I keep getting an error in the console: [Vue warn]: Invalid prop: type check failed for prop "date". Expected Number, got String. I've gone th ...

a pair of browser windows displaying a web application built with ASP.NET MVC

We are currently in the process of developing an ASP.NET MVC web application for internal use within our organization. Our users' productivity can greatly benefit from having a larger screen space, which is why we have decided to provide each user wit ...

A skeleton framework lacking a data storage backend

I am currently developing an offline javascript application that must be compatible with IE7, ruling out the use of localStorage. The app does not require any information persistence, as a refresh clears everything. My query is regarding setting up Backbo ...

Transition effects in the gallery are only triggered once all images have been seen

*Update 2 After identifying that the images were not fading in/out due to being considered unloaded, I resolved the issue by incorporating the initial 3 lines of jQuery code. I anticipate refactoring this and will provide an update once completed. JSFidd ...