Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:57:57+00:00 2026-06-17T16:57:57+00:00

I am trying to write a simple app that does the following: 1. User

  • 0

I am trying to write a simple app that does the following:
1. User inputs 2 parameters & clicks a button
2. Angular calls an external JAVA Servlet that returns JSON
3. App outputs the json string to the screen

I have a problem however, as when i click the button nothing happens. I believe (due to testing) that the reason this happens is that since the call is async, the variable that is returned is null.

Pertinent code:

controllers.js

function myAppController($scope,kdbService) {
    $scope.buttonClick = function(){
        var dat = kdbService.get($scope.tName,$scope.nRows);
        $scope.data = dat;

    }
}

services.js

angular.module('myApp.services', []).factory('kdbService',function ($rootScope,$http){
    var server="http://localhost:8080/KdbRouterServlet";
    return {
        get: function(tname,n){
            var dat;
            $http.jsonp(server+"?query=krisFunc[`"+tname+";"+n+"]&callback=JSON_CALLBACK").
                success(function(data, status, headers, config) {
                    console.log("1");
                    console.log(data);
                    dat=data;
                }).
                error(function(data, status, headers, config) {
                    alert("ERROR: Could not get data.");
                });
            console.log("2");
            console.log(dat);
            return dat;
        }
    }
});

index.html

<!-- Boilerplate-->
<h1>Table Viewer</h1>
<div class="menu" >
    <form>
        <label for="tName">Table Name</label>
        <input id="tName" ng-model="tName"><br>
        <label for="nRows">Row Limit</label>
        <input id="nRows" ng-model="nRows"><br>
        <input type="submit" value="Submit" ng-click="buttonClick()">
    </form>
</div>
{{data}}
<!-- Boilerplate-->

When i execute the code and push the button, nothing happens. However, if i look in my log, i see this:

2
undefined
1 
Object {x: Array[2], x1: Array[2]}

Clearly, what is happening is that the success function returns after the get function has returned. Therefore the object put into $scope.data is undefined, but the object returned from the jsonp call is left behind.

Is there a correct way to be doing this? Most of the tutorials I see assign the data to the $scope variable inside the success function, thereby skipping this problem. I want my service to be detached if possible.

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:57:58+00:00Added an answer on June 17, 2026 at 4:57 pm

    i would do something like that :

    controller

    function myAppController($scope,kdbService) {
        $scope.kdbService = kdbService;
        $scope.buttonClick = function(){
            $scope.kdbService.get($scope.tName,$scope.nRows);
    
        }
    }
    

    service

    angular.module('myApp.services', []).factory('kdbService',function ($rootScope,$http){
        var server="http://localhost:8080/KdbRouterServlet";
        return {
            data:{},
            get: function(tname,n){
                var self = this;
                $http.jsonp(server+"?
                query=krisFunc[`"+tname+";"+n+"]&callback=JSON_CALLBACK").
                    success(function(data, status, headers, config) {
                        console.log("1");
                        console.log(data);
                        self.data = data;
                    }).
                    error(function(data, status, headers, config) {
                        alert("ERROR: Could not get data.");
                    });
            }
        }
    });
    

    html

    {{kdbService.data}}
    

    OR

    use continuation in the get method :

    controller

    function myAppController($scope,kdbService) {
        $scope.buttonClick = function(){
            kdbService.get($scope.tName,$scope.nRows,function success(data){
               $scope.data = data;
            });
        }
    }
    

    service

        get: function(tname,n,successCallback){
            $http.jsonp(server+"?query=krisFunc[`"+tname+";"+n+"]&callback=JSON_CALLBACK").
                success(function(data, status, headers, config) {
                    successCallback(data,status,headers,config);
                }).
                error(function(data, status, headers, config) {
                    alert("ERROR: Could not get data.");
                });
        }
    

    OR use the $resource service

    http://docs.angularjs.org/api/ngResource.$resource ( you’ll need the angular-resource module

    code not tested.

    I want my service to be detached if possible.

    then put the “data object” in a “data service” calling a “data provider service”. You’ll have to call the “data provider service” somewhere anyway. There is no skipping this problem in my opinion, since that’s how javascript work.

    also use angular.controller("name",["$scope,"service",function($s,s){}]);

    so you will not need to care how parameters are called , as long as they are defined and injected properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple app that lets a user grant my code
I'm trying to write a simple web app that will allow me to write
I'm trying to write a simple app that has several internal pages. From #page2
I am trying to write a simple console app that POSTs to a page
I'm trying to write a very simple app that will do just one very
I am trying to write a simple android app that switches off my phone
I am trying to write (what should be) a simple app that has a
I'm new to cakephp and trying to write a simple app with it, however
I am trying to write a simple HTML 5 app for Windows Phone 7/7.5.
I'm trying to write my first simple mvc app. I have a Main View

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.