-1

Tried to read text from a local text file using jquery but if want to return a value from $.get, its not working. So I trie it like this

 function read() {
        var result=0;
            $.get("version.txt?_ts=" + new Date().getTime(), function (data) {
                var y1 = parseInt(data[0]);
                var y2 = parseInt(data[1]);
                var y = (y1 * 10) + y2;
                result=y;
            });
         return result;
        }

        function drr() {
            var d = read();
            document.write(d);
        }

but result value is remaining 0. could someone helpme with this

Krish
  • 45
  • 1
  • 8
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Arun P Johny Dec 11 '13 at 10:28
  • 1
    what is the difference between this and your [previous question](http://stackoverflow.com/questions/20514183/function-not-returning-value-to-other-function) – Arun P Johny Dec 11 '13 at 10:28
  • I want that data to be used to draw a graph. so I need it to be stored in a variable, in that function – Krish Dec 11 '13 at 11:14

3 Answers3

1

as $.get is similar as $.ajax, you might want to be looking into something I've done before.

I used to have the following code to return a value from an $.ajax request, but translated to your $.get.

function read (callback) {
    $.get("version.txt?_ts=" + new Date().getTime(), function (data) {
        var y1 = parseInt(data[0]);
        var y2 = parseInt(data[1]);
        var y = (y1 * 10) + y2;
        return callback(y);
    });
}

function readVersion () {
    read(function (result) { 
        console.log(result);
    });
}
lt.kraken
  • 1,287
  • 3
  • 10
  • 27
0

Try this

function read() {
        var result=0;
            $.get("version.txt?_ts=" + new Date().getTime(), function (data) {
                var y1 = parseInt(data[0]);
                var y2 = parseInt(data[1]);
                var y = (y1 * 10) + y2;
                return y;
            });
        }

        function drr() {
            var d = read();
            document.write(d);
        }
Amit
  • 15,217
  • 8
  • 46
  • 68
0

To read local files you need to use the HTML5 File API. No matter how your JS/jQuery/AJAX looks like...

Check out the API here

user1021726
  • 638
  • 10
  • 23