How to send javascript array to struts action using jQuery Ajax

I'm new to Struts 2. I want to send a javascript array to a Struts action class using a jQuery AJAX request.
The warning is working fine, execute()not working.
When I put System.out.println("language : "+ language);in the method execute(), the output

Language: null.

var langArr = [];
    $("#language").each(function()
    {
      var selectedLang = $("select").val();
      var selectedValues = $(this).val();
      langArr.push(selectedValues);
     });
    alert("Languages : " + langArr);

    $.ajax({
        method: "POST",
        url: "getProjectPost",
        data: { "language" : langArr },
        dataType : "json",
        traditional: true,
        success:
            function()
            {
             alert("Success");
            },
        error: 
           function()
            {
             alert("Error");
            }
    });

This is my action class.

public class ProjectPostAction {

    private int[] language;

    public final int[] getLanguage() {
        return language;
    }

    public final void setLanguage(int[] language) {
        this.language = language;
    }

    public String execute() throws Exception {          
           System.out.println("language : "+ language[0]);
           return "success";    
    }
+4
source share
2 answers

Jquery serializes data sent as parameters, using $.paraminternally when executing an ajax c request $.ajax.

, jQuery .

struts2 traditional, struts .

, , , .

, demo, , , $.ajax.

Struts2 , , , . , struts.

+2

JSON Struts2, - Struts2 JSON.

  • JSON JSP Json Result ,
  • Json Interceptor , JSON JSP , .
0

Source: https://habr.com/ru/post/1670053/


All Articles