ChazMcDingle
ChazMcDingle

Reputation: 675

Passing a parameter into an Ajax url in Scala Play

I have an Ajax route that takes in an optional string; I'm getting this string from a HTML select tag but when I pass the parameter into the url route it doesn't register as a variable.

$("#submit-button").on('click', function () {

var getOption = $( "#option" ).find(":selected").text(); 
var idToGet = $("#hi").val();
   $.ajax({
       url: '@routes.ApplicationController.getStuff(Some(getOption))', //requires an option[string]
       type: 'GET',
       data: {get_param: idToGet},
       dataType: 'json',
       success: function (data) {
       $.each(data, function (index, element) {
         alert("hi")
       });
     }
   });
 });

getOption can't be passed into the url route. I've tried implementing it as a function and calling it but it doesn't work. Any ideas?

Upvotes: 0

Views: 387

Answers (1)

Thomas Lehoux
Thomas Lehoux

Reputation: 1196

For a GET, the only way is to use absoluteURL and queryparam.

assuming your param name is option:

$("#submit-button").on('click', function () {

var getOption = $( "#option" ).find(":selected").text(); 
var idToGet = $("#hi").val();
   $.ajax({
       url: '@{routes.ApplicationController.getStuff(None).absoluteURL()}?option='+getOption, //requires an option[string]
       type: 'GET',
       data: {get_param: idToGet},
       dataType: 'json',
       success: function (data) {
       $.each(data, function (index, element) {
         alert("hi")
       });
     }
   });
 });

The drawback is, if you change the name of your parameter in Scala controller, you have to change it in the view.

Upvotes: 1

Related Questions