admin 管理员组文章数量: 1086019
i load inicio.jade with ajax req
$.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);
});
});
from this address in the server
app.get('/inicio',routes.inicio.inicio);
the problem is that i can access to the page with
how can i restrict the access only to ajax requests and redirect to 404 error page if is not an ajax request
i load inicio.jade with ajax req
$.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);
});
});
from this address in the server
app.get('/inicio',routes.inicio.inicio);
the problem is that i can access to the page with http://www.mypage./inicio
how can i restrict the access only to ajax requests and redirect to 404 error page if is not an ajax request
Share Improve this question asked Dec 31, 2012 at 2:56 andrescabana86andrescabana86 1,7888 gold badges32 silver badges56 bronze badges3 Answers
Reset to default 9With expressjs, you can respond only to xhr requests like this:
function handleOnlyXhr(req, res, next) {
if (!req.xhr) return next();
res.send({ "answer": "only is sent with xhr requests"});
}
(in your example, routes.inicio.inicio
would use the pattern above by checking req.xhr
)
You can detect whether it is an Ajax request at the server side by checking HTTP_X_REQUESTED_WITH
header. The value of HTTP_X_REQUESTED_WITH
header should be XmlHttpRequest
if it is an Ajax request.
You could override the beforeSend
event on the .ajax()
call. Per the documentation you can add custom headers to the request. This post gives an example.
Then you can have your page check for the presence of that custom header.
本文标签: javascripthow can i restrict the access to only ajax requestsStack Overflow
版权声明:本文标题:javascript - how can i restrict the access to only ajax requests? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744092848a2532357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论