1

I have made a tag input section on a page and want to save the tags as a user enters them.

So I am trying to pass a Javascript array of Strings to a spring controller.

This is javascript to populate the tagList array. It basically gives me back an array as so:

var taglist = [];
a[0] = hi;
a[1] = bye;
a[2] = shoe;

This is the way I try to do it.

  $(':submit').click(function() {
                                alert("array is "
                                        + JSON.stringify(tagList));
                                alert("enter ajax");
                                $.ajax({
                                    type : "POST",
                                    url : "/json/tags",
                                    data : {
                                        tagList : a

                                    },
                                    success : function(response) {
                                        alert("good results")
                                    },
                                    error : function(e) {
                                        alert('Error: ' + e);
                                    }
                                });
                            })

The list is being populated fine, it is just when I try to pass this List to my controller.

@RequestMapping(value = "/json/tags", method = RequestMethod.POST)
public String controllerMethod(@RequestParam(value="tagList[]") String[] myArray){
    System.out.println(myArray);

    if(myArray.length> 2){
        return "good";
    }else{
        return "bad";
    }
}

I was following this tutorial which does it with an Integer array, but can't seem to it to work with the String array.

Community
  • 1
  • 1
Blawless
  • 1,229
  • 2
  • 16
  • 26

1 Answers1

0

Needed to use a GET instead of POST and few other minor changes

                  alert("array is " + JSON.stringify(tagList));

                                var methodPN = 'getTagName';
                                <c:url var="getTagUrl" value="/json/tags.json" />
                                var ajaxURL = '${getTagUrl}';
                                var photoid = '${photo.uploadedFile.id}';

                                   var sendData = {
                                            controllerMethod: methodPN,
                                            tagList : tagList,
                                            photoid : photoid
                                        };
                                alert("enter ajax");

                                $.ajax({
                                    url : ajaxURL,
                                    data : sendData,
                                    type : "GET",           
                                      beforeSend: function (xhr) {
                                            xhr.setRequestHeader("Accept", "application/json");
                                            xhr.setRequestHeader("Content-Type", "application/json");
                                        },
                                    success : function(response) {
                                        alert("good results")
                                    },
                                    error : function(e) {
                                        alert('Error: ' + e);
                                    }
                                });
                            })

And my updated controller

@RequestMapping(value = "/json/tags", method = RequestMethod.GET, params = "controllerMethod=getTagName",
        produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String controllerMethod(Model model,@RequestParam(value="tagList[]", required = true) String[] myArray, @RequestParam(value="photoid", required = true) long photoid){
Blawless
  • 1,229
  • 2
  • 16
  • 26