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);
};
'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');