0

I'm trying to use the jQuery's getJSON function with Play framework. And I'm passing a query string with it. But it looks like it does not getting the value, only the key

Here's my jQuery function:-

<script type="text/javascript">
  $(function() {
    $("#button").click(function() {
        $.getJSON(
        '/getJsonResult',
        {'foo':'bar'},
        function(data) {
          $.each(data, function(i, result) {
            if(i != undefined) {

              var result_html = '<ul><li>';
              result_html    += result + '<\/li><\/ul>';

              $('#result_container').append(result_html);
            }
          });
        }
        );  
      });
  });
</script>

Here's the action method:-

public static Result getJsonResult() {
    Map queryParameters = request().queryString();

    List data = Arrays.asList("result", "This is just a test");

    if (queryParameters != null) {
    System.out.println("QS Key ---> " + queryParameters.containsKey("foo"));
    System.out.println("QS Value ---> " + queryParameters.containsValue("bar"));
    }

    return ok(Json.toJson(data));
}

The output:-

[info] play - Application started (Dev)
QS Key ---> true
QS Value ---> false
Switch
  • 14,783
  • 21
  • 69
  • 110
  • 1
    This may help:http://stackoverflow.com/questions/15907996/how-to-get-query-string-parameters-in-java-play-framework – mccannf Sep 22 '13 at 18:39

2 Answers2

1

It's because queryString method has Map type. Anyway, you can access value of "foo" key by calling queryParameters.get("foo").

hexagoncode
  • 141
  • 2
  • 6
0

I found the answer in How to get query string parameters in java play framework?

I had to use a Map type to get the relevant key, value pair.

Community
  • 1
  • 1
Switch
  • 14,783
  • 21
  • 69
  • 110