Search code examples
javascriptcodemirror

CodeMirror Import text with jQuery


I have the following function using the CodeMirror.

function app() {
    jQuery.get('http://localhost:2748/foo.txt', function (data) {
        alert(data);
    });
    alert(data);
    editor.setValue(data);
}

The alert displays the textfile data but when I assign them to the editor are not displayed. If I for example I make a new variable t = "test"; and then editor.setValue(t); then it works.


Solution

  • Your data is not available outside the callback function. Set the editor value inside your callback:

    function app() {
        jQuery.get('http://localhost:2748/foo.txt', function (data) {
            alert(data);
            editor.setValue(data);
        });
    }