我在 node.js 中有 html 标记,使用 jQuery 进行 ajax 调用有回调对象data
。
当我data
在控制台中输出时,一切正常,但是当我尝试输出到 DOM 时,我似乎无法将它作为变量引用。
我怎样才能使这项工作?我这样做对吗?
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);
};