【问题标题】:Get Action class result (return) String in JQuery ajax success在JQuery ajax成功中获取Action类结果(返回)字符串
【发布时间】:2025-12-22 08:05:12
【问题描述】:

在我的 JSP 页面中,我有一个表单和一些 div

<form id="personDetail" name="personDetail" action="personDetail">
<!-- input fields -->
</form>
<div id="sucess"></div>
<div id="error"></div>
<div id="edit"></div>
<div id="input"></div>

我正在使用 ajax 调用来提交表单。

在我的动作类中,我有以下代码

ActionClas

if(condition1)
{
//processing statements
return "success";
}
else if(condition2)
{
//processing statements
return "error";
}
else if(condition3)
{
//processing statements
return "input";
}
else if(condition4)
{
//processing statements
return "edit";
}

Ajax 调用

$(document).on('click', ".saveButton", function(){
        var formId='personDetail';
        var form = $("#"+formId);
        form.submit(function ()
        {
                var urlAction=form.attr('action');
                var dataFields=form.serialize();
                $.ajax({
            type: "post",
            url: urlAction,
            data: dataFields,
            success: function (data) {
                    var ajaxResult= $('<div/>').html(data),
                    $ajaxContent = ajaxResult.find('#ajaxContent');
                var ajaxActionResult=$('<div/>').html($ajaxContent);
                $("#success").html(ajaxActionResult);
            }
        });
         return false;
    });
});

在 struts.xml 中,我为每个结果都提到了一些 JSP,它在 Ajax 中返回。

所有都返回成功并获得 HTML 内容。我用 id="success" 设置为 DIV。所以所有结果都设置为该 DIV。

我想在适当的 div 中显示每个结果。也就是说,如果动作类返回字符串是error,我想将结果设置为带有id="error"的Div,如果是input , 然后结果应该设置为带有 id="input" 的 DIV,其他的也类似。

由于所有结果在 Ajax 中都返回成功,因此在 ajax 中也使用 Error 没有意义。而且也不能编辑和输入。

我怎样才能做到这一点?

谢谢

【问题讨论】:

    标签: jsp jquery


    【解决方案1】:

    请参考:Getting return value from action with jquery.forms.js options...MVC3

    PS:只需更改您的操作类以返回 Json 字符串.. 并在您的“afterSubmit”函数中使用该特定键(response.YOUR_KEY ==“YOUR_TEST_VALUE”)以获得所需的输出。

    【讨论】:

      最近更新 更多