如何通过 POST (Ajax) 发送 JSON 数据并从 Struts 2 操作接收 JSON 响应

How to send JSON data via POST (Ajax) and receive JSON response from Struts 2 action(如何通过 POST (Ajax) 发送 JSON 数据并从 Struts 2 操作接收 JSON 响应)

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

问题描述

我试图了解如何使用 JSON,并且在此过程中我试图从 Struts2 操作中获取 JSON 响应并显示响应警报.为此,我在 JavaScript 中使用 Ajax POST,如下所示:

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");
              }
            });
}

我的 Action 类包含一个 String 字段,我试图在警报中显示为 JSON 响应.但我发现这样做有问题.我错过了什么或做错了什么?

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?

我的动作类如下:

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 文件:

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>

我的 pom.xml:

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

推荐答案

我通过在 json 结果上添加 myMsg 解决了我的问题.感谢大家的帮助

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

这篇关于如何通过 POST (Ajax) 发送 JSON 数据并从 Struts 2 操作接收 JSON 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何通过 POST (Ajax) 发送 JSON 数据并从 Struts 2 操作接收 JSON 响应

基础教程推荐