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?

  1. use inspector tool check rendered html, understand
  2. check in jquery docs, how pass params ajax requests
  3. get little bit more familiar routing , using js in framework's views

tips:

  1. make search param in route optional one

    get /autocomplete  controllers.application.autocomplete(search: string ?= "") 
  2. use 'almost normal' way of building ajax queries:

    // cut... $.get("@routes.application.autocomplete()",      { search: search },      function (data) {         //some stuff ...      } ); // cut... 
  3. validate incoming value if isn't empty or null , return badrequest() (probably in json format) in such case.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -