Re: How to pass a variable from Javascript code to Java

Numa Schmeder <[email protected]>
Newsgroups gmane.comp.java.tapestry.user
Message-ID <[email protected]>
Hello Marwa,

The answer provided by Thiago is much simpler. 
Just put your data in a data attribute of a tag and then use the tutorial ajax and zones to send this data to a server side event handler. 
If you want to do it automatically without triggering a javascript event, just put you script in an on document ready event, to make sure your javascript code is called once the page is rendered and loaded by the browser. 


===> in html page

<div id=“myId” data-mydata=“mydata"></div>

===> javascript at the end of html page

$(function() {
	var dataToSend = $(‘#myId’).data(‘mydata’);
	ajax(‘pageData', { 
			data: dataToSend, // This doesn't need to be the same element as the one two lines above
            // Callback called when the request is finished. 
            // response.json is the object returned by the event handler method
			success: function(response) {
				alert('sent to server');
			}
		});
});


=====> in java page

JSONObject onPageData()
    {
        return new JSONObject("origin", "componentAction");
    }  


You can use jquery or something else.

Best
Numa



  <http://www.dfacto.ch/>	Numa Schmeder    www.dfacto.ch  <http://www.dfacto.ch/>
[email protected] <mailto:[email protected]>   |   M +41 79 538 30 01 

DIGITAL STRATEGY   |   DESIGN   |   DEVELOPMENT


 

> Le 28 août 2018 à 12:22, marwa hussein <[email protected]> a écrit :
> 
> Hello,
> 
> Thanks all for your suggestions. I followed the example shown in
> https://tapestry.apache.org/ajax-and-zones.html in "Invoking server-side
> event handler methods from JavaScript"   but here the event is in the java
> code "server-side" and is invoked from the Javascript code "onClick()" ,
> but what I want is the opposite direction, sending a "string variable" from
> a tag appended in  the clientside javascript code to the java code "in the
> serverside".
> For now, I will test to use hidden input (although I didn't want to use a
> form submit) and I will see if I can make it or not ...
> 
> Thank you all for your help and valuable suggestions, and of course, if
> anyone face the same problem before and could give me hints to how to do it
> please tell me.
> Regards,
> Marwa
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.