0

I am trying to read a local file on the server with a standard function loadDoc(url, cfunc), then

1) search for a particular string in the file (getLine());

2) if possible, store that line to a variable.

For point 1 I pass a string to the callback. 2) Getting the response is problematic because XMLHTTPRequest is asynchronous. At this moment the error is: "ReferenceError: xhttp is not defined"

function main(){
    var url="data.txt"
    var str="1,0,"; //just an example
    var myCallBackWithVar = function(){
        getLine(str);
    };
    loadDoc(url, myCallBackWithVar);

    //Can I get the line here somehow?
}

function loadDoc(url, cfunc) {
    var xhttp=new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
        cfunc(xhttp);
        }
    }
    xhttp.overrideMimeType('text/plain');
    xhttp.open("GET", url, true);
    xhttp.send();
}

//Find string with the desired data in txt file
function getLine(str) {
    var data=xhttp.responseText;
    //Find the line from the txt file
    var start=data.indexOf(str);
    var end=data.indexOf(";",start);
    var line=data.substring(start,end);    
    return line;
}

data.txt is something like this:

some data here
0,0,9;
1,0,10;
1,1,11;

I have already tried to pass the XMLHTTPRequest objetct getLine(xhttp,str). How to solve points 1 and 2? I'd rather keep it jQuery free for the moment. Thanks

2 Answers 2

1

Can I get the line here somehow?

I don't think that's a good idea. You can't be sure that your app will work correctly. XHR is a async function and you should use async architecture.

Here the example how this functionality can be done.

    var text; // define global variable
    var str = "1,0,"; //just an example

    function main(){
        var url = "data.txt";
        var cb = function (data){
            text = getLine(data);
            // you can use text var here
            // or in anyewhere in your code
        }

        loadDoc(url, cb);
    }

    function loadDoc(url, cb) {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                cb(xhr.responseText);
            }
        }
        xhr.overrideMimeType('text/plain');
        xhr.open("GET", url, true);
        xhr.send();
    }

    //Find string with the desired data in txt file
    function getLine(data) {
        if(data) {
            //Find the line from the txt file
            var start = data.indexOf(str);
            var end = data.indexOf(";", start);
            var line = data.substring(start, end);

            return line;
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

In getLine I get str is not defined even if I add the definition of str in main. how to pass that as well?
Updated. If you define a variable inside a function that var will be visible only in that function. To define global variable you need to use window object like this window.myVar = 'something'; Or just define variable in global scope (not inside a function).
1

On complete, you don't need to pass the whole xhttp variable through too the callback function. When you do this:

function getLine(str) {
    var data=xhttp.responseText;

xhttp is already out of scope. To fix this, the parameter name would also have to be xhttp.

A better way would be to do :

cfunc(xhttp.responseText);

and then

var data=str

This way, you are passing only what you need as an argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.