后端其實並不需要知道前端發起的請求是不是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); });