我用ajax req加载inicio.jade
$.ajax(
{
url:'/inicio',
cache: false,
type: 'GET',
}).done(function(web)
{
$('#contenido_nav_principal').fadeOut(600,function()
{
$('#contenido_nav_principal').empty();
$('#contenido_nav_principal').html(web);
$('#navegacion').animate({height:'600px'},200);
$('#contenido_nav_principal').fadeIn(600);
});
});
从服务器中的该地址
app.get('/inicio',routes.inicio.inicio);
问题是我可以使用http://www.mypage.com/inicio访问该页面
如何将访问限制为仅对ajax请求进行访问,如果不是ajax请求,则如何重定向至404错误页面
最佳答案
使用expressjs,您只能响应以下xhr请求:
function handleOnlyXhr(req, res, next) {
if (!req.xhr) return next();
res.send({ "answer": "only is sent with xhr requests"});
}
(在您的示例中,
routes.inicio.inicio
将通过检查req.xhr
使用上述模式)