Playframework 2 using Jquery to request from server -
i want request jquery application method wich has string arg, have problem passing value of string. in html have script:
<script> var search; $("#search").keyup(function () {     if($(this).val() != search){         search = $(this).val();         //alert(search);         $.get("@routes.application.autocomplete("+search+")", function (data) {             //some stuff done here            });         return false;     } }).keyup(); my application method like:
public static result autocomplete(string search) {     system.out.println("searching for: "+search);     final list<string> response = new arraylist<string>();     response.add("test1");     response.add("test2");     return ok(json.tojson(response));  } what happens jquery not sending value of search var instead sending string "search".
how can concatenation of string inside jquery get()?
thanks
yo can not mix server-side variables (ie. @routes.c.a(somevar)) typical client-side elements, that's obvious!  isn't it?
- use inspector tool check rendered html, understand
- check in jquery docs, how pass params ajax requests
- get little bit more familiar routing , using js in framework's views
tips:
- make - searchparam in route optional one- get /autocomplete controllers.application.autocomplete(search: string ?= "")
- use 'almost normal' way of building ajax queries: - // cut... $.get("@routes.application.autocomplete()", { search: search }, function (data) { //some stuff ... } ); // cut...
- validate incoming value if isn't - emptyor- null, return- badrequest()(probably in json format) in such case.
Comments
Post a Comment