Sending string to Javascript to java method in Play Framework

I am working on a system for my university and I am using a replay platform for this. The administrator of this system sets a marker on google map and I get coordinates from that point.

Now I am trying to pass this information to the server so that I can store them in Strings in mySQL database. The only problem I am facing is passing data from my string in javascript / JQuery to a java function.

I tried different solutions on the internet, but some of them looked outdated and I couldn't figure out how to do it.

I have only programmed in Java, Javascript, JQuery and PHP and have never used AJAX (like the $ .get () method from JQuery), but I think it might be very similar to what I know from PHP.

eg. http://java.dzone.com/articles/jquery-ajax-play-2

I would like to pass my String using a button by clicking on my java function to store it in my db.

I am really confused about this.

I know I can use something like

<a href="{@routes.Application.postMethod()}"> Send </>

and then specify the function in routes like

POST / post controllers .Application.post ();

but how to pass the string to qjuery? and how can i store String as string in java function like:

public static Result post(String Lat, String Lng){

???????????? EVOLUTION NEEDED  ?????

}

      

Thanks in advance, I really need your help :)!

+3


source to share


1 answer


I don't understand why you are doing POST, as it can be done with a GET request.

According to this example: http://www.playframework.com/documentation/1.2/ajax#jsaction

We can see that Play makes it easy to use the jsAction tag. Suppose you have the following route:

GET     /admins/marker        Admins.marker

      

Then, in your HTML, at the bottom, you do something like:



<script type="text/javascript">
   $('#myButton').click( function() {
   var action = #{jsAction @marker(':latitude',':longitude') /}
   $('#result').load(
       action({latitude: '23', longitude: $('#longitudeField').val }), 
       function() {
           $('#content').css('visibility', 'visible')
       }
   )
   });
</script>

      

In this case, the request will be sent like (example):

GET /admins/marker?latitude=23&longitude=67

      

And then on your backend you need java fn to deal with this route.

Basically javascript / jquery is called when the #myButton element is clicked, then we generate the route url we are going to do with jsAction, then we do using load to make a GET request. You can also change this message if you like.

0


source







All Articles