express响应前端ajax请求

后端其实并不需要知道前端发起的请求是不是ajax,后端只需要响应请求即可.
例子:
前端这样写:
$('button').on('click', function(event) {
	event.preventDefault();
	/* Act on the event */
	$.ajax({
		url: '/ajax/test',
		type: 'get',
		dataType: 'json',
		success:function(data){
			$('div').html(data.tips);
		},
		error:function(data){
			alert('error');
		}
	});

});
后端就这么写:
app.get('/ajax/test',function(req,res){
  var ajaxTest={
    tips:"you are not alone"
  };
  res.send(ajaxTest);
});
原文地址:https://www.cnblogs.com/xuange306/p/5975828.html