如何通过POST(阿贾克斯)发送JSON数据,并接收来自Struts 2的动作JSON响应 [英] How to send JSON data via POST (ajax) and receive json response from Struts 2 action

查看:176
本文介绍了如何通过POST(阿贾克斯)发送JSON数据,并接收来自Struts 2的动作JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何使用JSON和在这个过程中,我试图从Struts2的行动JSON响应并显示报警响应。对于这个,我使用Ajax POST在JavaScript如下:

 函数checkButtonClick(ID){

        VAR btnSave ='saveAttendees';
            VAR atNameList = $('#attName'+ id)的.VAL();
            VAR ptNameList = $('#postName'+ id)的.VAL();
            变种AID = $('#at_id'+ id)的.VAL();

            警报(这里);
            VAR ARR = {buttonName:btnSave,
                    attendeesNameList:atNameList,
                    attendeesPostList:ptNameList,
                    hidden_​​At_id:援助
                    };
            $阿贾克斯({
                数据:ARR,
                键入:POST,
                数据类型:JSON,
                网址:meeting_record_form

                成功:函数(结果){
                    警报(result.myMsg);
              },
                错误:函数(结果){
                    警报(错误);
              }
            });
}
 

我的动作类包含一个字符串字段,我想在警报为JSON响应显示。但是,我发现问题这样做。我在想什么或者做错了吗?

我的动作类如下:

 私人字符串myMsg;

    公共字符串getMyMsg(){
        返回myMsg;
    }

    公共无效setMyMsg(字符串myMsg){
        this.myMsg = myMsg;
    }

私人字符串updateAttendeesRecord(){


        meetingRecordService.updateAttendeesRecord(attendeesListMethod(),meeting_record);
        setMyMsg(更新成功!);
            返回成功;
    }
 

的struts.xml 文件:

 <包名称=默认扩展=支柱默认,JSON-默认>
    <结果类型>
      <结果类型名称=JSON级=org.apache.struts2.json.JSONResult/>
    < /结果类型>
    <拦截>
      <拦截器的名字=JSON级=org.apache.struts2.json.JSONInterceptor/>
    < /拦截>

    <作用NAME =meeting_record_form级=com.task.action.MeetingRecordAction方法=updateAttendeesRecord>
     <结果名称=成功TYPE =json的/>
    < /作用>
< /包>
 

的pom.xml

 <依赖>
    <的groupId> org.apache.struts< /的groupId>
    < artifactId的> Struts2的JSON的,插件和LT; / artifactId的>
    <版> 2.3.15< /版本>
< /依赖性>
 

解决方案

我通过JSON结果将myMsg解决我的问题。感谢所有帮助

I am trying to understand how to use JSON and in the process I'm trying to get a JSON response from Struts2 action and display an alert for the response. For this I'm using Ajax POST in JavaScript as follows:

function checkButtonClick(id){  

        var btnSave = 'saveAttendees';  
            var atNameList = $('#attName'+id).val();
            var ptNameList = $('#postName'+id).val();
            var aId = $('#at_id'+id).val();

            alert("here");
            var arr = {buttonName:  btnSave,
                    attendeesNameList: atNameList,
                    attendeesPostList: ptNameList,              
                    hidden_At_id: aId
                    };
            $.ajax({                            
                data: arr,
                type: 'POST',
                dataType: 'json',               
                url:"meeting_record_form",

                success:function(result){
                    alert(result.myMsg);
              },
                error:function(result){
                    alert("error");
              }
            });
}

My action class contains a String field that I'm trying to display in alert as JSON response. But I'm finding problem doing this. What am I missing or doing wrong?

My action class is as follows:

private String myMsg;

    public String getMyMsg() {
        return myMsg;
    }

    public void setMyMsg(String myMsg) {
        this.myMsg = myMsg;
    }

private String updateAttendeesRecord() {


        meetingRecordService.updateAttendeesRecord(attendeesListMethod(), meeting_record);
        setMyMsg("Update Successful!");
            return SUCCESS;
    }

struts.xml file:

 <package name="default" extends="struts-default, json-default">
    <result-types>
      <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>
    <interceptors>
      <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
    </interceptors>

    <action name="meeting_record_form" class="com.task.action.MeetingRecordAction" method="updateAttendeesRecord">
     <result name="success" type="json" />
    </action>
</package>

My pom.xml:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>2.3.15</version>
</dependency>

解决方案

I've solved my problem by adding myMsg on the json result. Thanks for all the help

这篇关于如何通过POST(阿贾克斯)发送JSON数据,并接收来自Struts 2的动作JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆