【问题标题】:Get Action method result status in Ajax在 Ajax 中获取 Action 方法结果状态
【发布时间】:2013-07-05 05:11:56
【问题描述】:

情况是这样的

在我的 JSP 页面中,我正在调用一个动作类方法

<input type="text" class="inputstyle" name="feedName" id="feedName" placeholder="<s:text name="global.feed_name" />" required>

这是我的 Ajax JQuery

$(document).ready(function() {
        $('#feedName').blur(function() {
            var feedName=$("#feedName").val();
            if(feedName!="")
            {
                $.ajax( {
                      traditional: true,
                      type: "POST",      
                      url: "feedCheck",
                      data:"feedName="+feedName,
                      dataType: "text",
                      success: function(response) {
                          alert("AVAILABLE!");
                      },
                      error: function(data) {
                          alert("NOT AVAILABLE!!!");
                      }       
                    });
            }
        });
    });

struts.xml

<action name="feedCheck" method="feedCheck" class="com.analytic.webapp.action.AAIDCAIndexAction">
            <result name="success">DCAAnalytix.jsp</result>
            <result name="error">DCAAnalytix.jsp</result>
        </action>

动作类方法

public String feedCheck()
    {
        MClient client = (MClient) getRequest().getSession().getAttribute(
                AAI_CLIENT);
        List<String> feedNamesFromDB=mFeedManager.getAllFeedNameByClient(client.getClientKey());
        System.out.println(feedName);
        if(feedNamesFromDB.size()>0){
            if(feedNamesFromDB.contains(feedName)){
                return ERROR;
            }
        }
        return SUCCESS;
    }

ajax 调用工作正常并调用动作类方法并执行。但问题是,结果总是在 Ajax 中出错。也就是说,如果该方法返回 SUCCESS,那么网页也会发出 “NOT AVAILABLE!!!”

警报

我是阿贾克斯的新手。当我搜索时,大多数帖子也是关于返回 JSON 数据的。我不想要 JSON 数据。我只需要结果状态以及如何在 Ajax 中获得它?

【问题讨论】:

  • 您使用的是哪个版本的 struts?

标签: java jsp jquery struts2


【解决方案1】:

struts2中ajax的结果类型应该是流。在你的代码中试试这个。

<action name="feedCheck" method="feedCheck" class="com.analytic.webapp.action.AAIDCAIndexAction">
  <result type="stream">
    <param name="contentType">text/html</param>
    <param name="inputName">inputStream</param>
  </result>
</action>

在你的行动课上。 您应该使用 getter 和 setter 创建类变量

private InputStream inputStream;

然后在你的方法中

public String feedCheck()
{
    MClient client = (MClient) getRequest().getSession().getAttribute(
            AAI_CLIENT);
    List<String> feedNamesFromDB=mFeedManager.getAllFeedNameByClient(client.getClientKey());
    System.out.println(feedName);
    if(feedNamesFromDB.size()>0)
    {
        if(feedNamesFromDB.contains(feedName))
        {
            this.setInputStream(new ByteArrayInputStream(ERROR.getBytes()));
        }
        else
        {
            this.setInputStream(new ByteArrayInputStream(SUCCESS.getBytes()));
        }
    }
    else
    {
        this.setInputStream(new ByteArrayInputStream(SUCCESS.getBytes()));
    }

    return SUCCESS;

}

希望这会奏效。

在你的视图页面中

$(document).ready(function() {
        $('#feedName').blur(function() {
            var feedName=$("#feedName").val();
            if(feedName!="")
            {
                $.ajax( {
                      traditional: true,
                      type: "POST",      
                      url: "feedCheck",
                      data:"feedName="+feedName,
                      dataType: "text",
                      success: function(data, success) {
                          if(data.indexOf("success")==-1){
                                  alert("Action returned Error")
                           }else{
                                 alert("Action returned Success") 
                           }
                      }      
                    });
            }
        });
    });

流数据将返回字符串“error”或“success”。并且会在你的ajax在view page的成功方法中可用。

【讨论】:

  • 谢谢。现在它只会成功。即使在 Action 类方法中设置为错误也不会出错(总是显示 "AVAILABLE!" 警报)。我需要更改 Ajax Jquery 脚本中的任何内容吗?谢谢
  • 第一个条件总是正确的。即if(response.indexOf("success")==-1) 始终为真并提醒其相应的消息
  • 请在您的操作方法中进行调试,看看它返回了什么。从该方法返回的字符串将仅在您的 ajax 成功中可用。也可以通过mozilla的firebug监控ajax的请求和响应。
  • 我做了调试,当条件为真时它返回错误。我做了alert(response.indexOf("success")); alert(response.indexOf("error")); 并且两者都显示为 "-1" 这就是为什么 if 中的内容正在显示
  • 答案已编辑请检查jsp页面中的ajax函数是否成功:function(data,success){'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2017-06-05
相关资源
最近更新 更多