0

I have html markup in node.js, using jQuery for an ajax call there is callback object data.
When i output data in console every thing works well, but when i try to output into the DOM i cant seem to reference it as a variable.
How can i make this work? Am i going about this correctly?

exports.serve = function(req, res) {
var content = '<html>\
<head></head>\
<script src="http://code.jquery.com/jquery.min.js"></script>\
<script type="text/javascript">\
$(function(){\
    $(".ajaxLI a").click(function(){\
        var mypost = "abc123";\
        $.post( "/ajaxcall?myget=zzz", {mypost:mypost}, function(data){\
            //console.log(data);\n\ // << this works
            $("body").append("<h1>data</h1>");\n\ // << this does not
            //$("body").append("<h1>'+data+'</h1>");\n\ //<< error
        });\
        return false;\
    });\
});\
</script>\
<body>\
<h1>INDEX</h1>\
<ul>\
    <li><a href="./index">index</a></li>\
    <li class="ajaxLI"><a href="./ajaxcall">ajax</a></li>\
    <li><a href="./login?var=123abc">login</a><< variables</li> \
    <li><a href="./admin/login">admin_login</a></li>\
    <li><a href="./admin/index">admin_index</a></li>\
</ul>\
</body>\
</html>';
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(content);
};

1 Answer 1

1

'data' is literally the string data. You have to concatenate the strings (with consistent quotes):

$("body").append("<h1>" + data + "</h1>");

Or:

$('<h1>', {html: data}).appendTo('body');
Sign up to request clarification or add additional context in comments.

4 Comments

this gives an error ` $("body").append("<h1>'+data+'</h1>");\n` ^ ReferenceError: data is not defined
@tq: You have to use consistent quotes. Either both ' or both ".
the quotes are consistent, if you look above the entire markup is within single quotes
i see what you mean, i was breaking out of the single quote and it was looking for a variable outside of the markup

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.