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 7598635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:27:29+00:00 2026-05-30T22:27:29+00:00

In my application i’m isolating the networking in a method and performing a callback

  • 0

In my application i’m isolating the networking in a method and performing a callback to get the JSON response , how can i use it afterward ?

getJson(url, acallback);

function getJson(url, callback) {
    Ti.API.info(" im here " + url);
    var jsonObject;
    var xhr = Ti.Network.createHTTPClient();
    xhr.setTimeout(3000);
    xhr.onload = function () {
        var jsonObject = eval('(' + this.responseText + ')');
        callback.call(jsonObject)
    }
    xhr.open("GET", url);
    xhr.send();
    Ti.API.info(" passed ");
};

function acallback() {
    return this;
}

Now my questions is , how can i use the returned output afterward ?

  • 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-05-30T22:27:30+00:00Added an answer on May 30, 2026 at 10:27 pm

    Should look like this:

    function getJson(url, callback) {
        Ti.API.info(" im here " + url );
        var jsonObject, xhr = Ti.Network.createHTTPClient();
    
        xhr.setTimeout(3000);
        xhr.onload = function() {
            var jsonObject = JSON.parse(this.responseText);
            callback(jsonObject)
        }
        xhr.open("GET" , url); 
        xhr.send(); 
        Ti.API.info(" passed " );
    }
    
    function acallback(json) {
          Ti.API.info("data from ajax: " + json);
    }
    
    getJson(url , acallback);
    

    Notice that I removed the use of eval since it’s bad practice to use that, as it says (here):

    The eval function is very fast. However, it can compile and execute
    any JavaScript program, so there can be security issues. The use of
    eval is indicated when the source is trusted and competent. It is much
    safer to use a JSON parser. In web applications over XMLHttpRequest,
    communication is permitted only to the same origin that provide that
    page, so it is trusted. But it might not be competent. If the server
    is not rigorous in its JSON encoding, or if it does not scrupulously
    validate all of its inputs, then it could deliver invalid JSON text
    that could be carrying dangerous script. The eval function would
    execute the script, unleashing its malice.

    Also, you better use the var keyword only once per scope.


    Edit

    If you want to use the resulting json object from outside the scope of the callback, just define the callback in the scope where the json is needed, for example:

    var obj = {
        x: 4,
        doit: function() {
            var _this = this;
            var callback = function(json) {
                alert(_this.x * json.y);
            };
            getJson(url , callback);
        }
    }
    

    The json.y part I just made up for the example of course.


    2nd Edit

    Alternatively, if you want to use the call option, you can do this:

    function getJson(url, callback, bindto) {
        Ti.API.info(" im here " + url );
        var jsonObject, xhr = Ti.Network.createHTTPClient();
    
        xhr.setTimeout(3000);
        xhr.onload = function() {
            var jsonObject = JSON.parse(this.responseText);
            callback.call(bindto, jsonObject)
        }
        xhr.open("GET" , url); 
        xhr.send(); 
        Ti.API.info(" passed " );
    }
    
    var obj = {
          x: 5
    }
    
    function myCallback(json) {
          alert(this.x * json.y);
    }
    
    getJson(url, myCallback, obj);
    

    3rd Edit

    If we’re on the subject, I recommend using a nice trick, which is used in Prototype, MooTools, jQuery and according to the MDN was Introduced in JavaScript 1.8.5.

    Function.prototype.bind = function(scope) {
      var _function = this;
    
      return function() {
        return _function.apply(scope, arguments);
      }
    }
    

    You can read the tutorial where I copied to code from.

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

Sidebar

Related Questions

my application use 10 threads that to read a lot of html file.similar the
Application: This is a workshop proposal system for a conference. A user can create
The application crashes on the row: [uids addObject:user.uid]; Who can tell why??
Application use NHibernate. I Have object A that contains set of objects B. I
My application has a complex schema for the domain entity. It is required use
Application crashes on trying to access [tableView cellForRowAtIndexPath:indexPath] in the heightForRowAtIndexPath delegate method. -
My application doesn't oriented even if I use - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Override to
Application.ProductVersion is not showing the incremental version. can anybody help me how to perform
in Application tab of project properties window and in startup objects combobox, I can
Application configuration: Web application using java first method of creating JAX-WS 2.0 Web Services

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.