<small id='y731U'></small><noframes id='y731U'>

    1. <legend id='y731U'><style id='y731U'><dir id='y731U'><q id='y731U'></q></dir></style></legend>

        <i id='y731U'><tr id='y731U'><dt id='y731U'><q id='y731U'><span id='y731U'><b id='y731U'><form id='y731U'><ins id='y731U'></ins><ul id='y731U'></ul><sub id='y731U'></sub></form><legend id='y731U'></legend><bdo id='y731U'><pre id='y731U'><center id='y731U'></center></pre></bdo></b><th id='y731U'></th></span></q></dt></tr></i><div id='y731U'><tfoot id='y731U'></tfoot><dl id='y731U'><fieldset id='y731U'></fieldset></dl></div>
        • <bdo id='y731U'></bdo><ul id='y731U'></ul>
      1. <tfoot id='y731U'></tfoot>

        如何判断一个请求是Ajax还是Normal?

        How to determine whether a request is Ajax or Normal?(如何判断一个请求是Ajax还是Normal?)
      2. <i id='zGqFL'><tr id='zGqFL'><dt id='zGqFL'><q id='zGqFL'><span id='zGqFL'><b id='zGqFL'><form id='zGqFL'><ins id='zGqFL'></ins><ul id='zGqFL'></ul><sub id='zGqFL'></sub></form><legend id='zGqFL'></legend><bdo id='zGqFL'><pre id='zGqFL'><center id='zGqFL'></center></pre></bdo></b><th id='zGqFL'></th></span></q></dt></tr></i><div id='zGqFL'><tfoot id='zGqFL'></tfoot><dl id='zGqFL'><fieldset id='zGqFL'></fieldset></dl></div>

              <tbody id='zGqFL'></tbody>
            <legend id='zGqFL'><style id='zGqFL'><dir id='zGqFL'><q id='zGqFL'></q></dir></style></legend>

              • <bdo id='zGqFL'></bdo><ul id='zGqFL'></ul>
              • <small id='zGqFL'></small><noframes id='zGqFL'>

                <tfoot id='zGqFL'></tfoot>

                1. 本文介绍了如何判断一个请求是Ajax还是Normal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想对 AJAX 请求和普通请求以不同的方式处理错误.

                  I want to handle errors differently for AJAX requests vs normal requests.

                  如何在 Struts2 操作中识别请求是否为 AJAX?

                  How do I identify whether a request is AJAX or not in Struts2 actions ?

                  推荐答案

                  您应该检查请求标头 X-Requested-With 是否存在并且等于 XMLHttpRequest.

                  You should check if the Request Header X-Requested-With is present and equals to XMLHttpRequest.

                  请注意,并非所有 AJAX 请求都有此标头,例如 Struts2 Dojo 请求不发送它;如果您使用 Struts2-jQuery(或任何其他新的 AJAX 框架)生成 AJAX 调用,它就在那里.

                  Note that not all the AJAX requests have this header, for example Struts2 Dojo requests don't send it; if you instead are generating AJAX calls with Struts2-jQuery (or with any other new AJAX framework), it is there.

                  您可以使用 Firebug 的 Net 模块来检查它是否存在...例如,当您在 Stack Overflow 上投票时;)

                  You can check if it's present by using Firebug's Net module... for example, when you vote on Stack Overflow ;)

                  要从 Struts2 Action 中检查它,您需要实现 ServletRequestAware 接口,然后获取 Request 并检查该特定标题是这样的:

                  To check it from within a Struts2 Action, you need to implement the ServletRequestAware interface, then get the Request and check if that particular header is there like this:

                  public class MyAction extends ActionSupport implements ServletRequestAware {
                     private HttpServletRequest request;
                  
                     public void setRequest(HttpServletRequest request) {
                        this.request = request;
                     }
                  
                     public HttpServletRequest getRequest() {
                        return this.request;
                     }
                  
                     public String execute() throws Exception{
                        boolean ajax = "XMLHttpRequest".equals(
                                        getRequest().getHeader("X-Requested-With"));
                        if (ajax)
                           log.debug("This is an AJAX request");
                        else 
                           log.debug("This is an ordinary request");
                  
                        return SUCCESS;
                     }  
                  }
                  

                  注意,你也可以通过 ActionContext 获取请求,不需要实现 ServletRequestAware 接口,但不是推荐的方式:

                  Note that you can obtain the request via ActionContext too, without implementing the ServletRequestAware interface, but it is not the recommended way:

                  HttpServletRequest request = ServletActionContext.getRequest();
                  

                  这篇关于如何判断一个请求是Ajax还是Normal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)
                  <i id='SAV8r'><tr id='SAV8r'><dt id='SAV8r'><q id='SAV8r'><span id='SAV8r'><b id='SAV8r'><form id='SAV8r'><ins id='SAV8r'></ins><ul id='SAV8r'></ul><sub id='SAV8r'></sub></form><legend id='SAV8r'></legend><bdo id='SAV8r'><pre id='SAV8r'><center id='SAV8r'></center></pre></bdo></b><th id='SAV8r'></th></span></q></dt></tr></i><div id='SAV8r'><tfoot id='SAV8r'></tfoot><dl id='SAV8r'><fieldset id='SAV8r'></fieldset></dl></div>
                    <tbody id='SAV8r'></tbody>
                  <tfoot id='SAV8r'></tfoot>
                    <legend id='SAV8r'><style id='SAV8r'><dir id='SAV8r'><q id='SAV8r'></q></dir></style></legend>

                  1. <small id='SAV8r'></small><noframes id='SAV8r'>

                      • <bdo id='SAV8r'></bdo><ul id='SAV8r'></ul>