我有一个动作不同的逻辑依赖,如果请求是一个AJAX一个或没有。
(对于AJAX登录,我不需要登录成功,这是不是在正常登录的情况下,例如后重定向)。
什么是最好的方式,检查标题为 X-要求,随着身旁:XMLHtt prequest
是否有标志的东西?
I have different logic in an action depends if the request is an AJAX one or not.
(For AJAX logins, I do not need to redirect after successful login, which is not the case in normal login, for example).
What is the best way, beside checking the headers for X-Requested-With: XMLHttpRequest
Is there a flag or something?
推荐答案
这个方法通过检查这几乎设置(如果不是)所有主要的JS库,包括jQuery和YUI的。
This method works by checking for a header which is set by almost (if not) all of the major JS libraries, including jQuery and YUI.
$this->getRequest()->isXmlHttpRequest() //returns true if is XHR
由smack0007详述的方法,保证是准确的,但上述方法是正常,如果连接是由一个库,它设置首标总是制成。它可能是不适合公共API。
The method detailed by smack0007 is guaranteed to be accurate, but the method above is fine if the connection is always made by a library which sets the header. It is probably not suitable for a public API.
相关推荐
最新文章