|
||||||||
Sending Javascript (client side) data to a Server Program .......e.g. sending data about a Facebook user from client to your app's server program
If you want to send javascript data (variable values) to a Servr Program (like JSP or Servlet or any server program) -- you have to understand that JavaScript is client side technology so even if inside a JSP the JSP does not have access to its WHAT DO YOU DO? You need to send the data via a HTTP request to the Server Program with parameters in the request (name/value)
EXAMPLE SCENARIO: you have a Facebook Tweets app and you want to
|
Special NOTE: if you are using JQuery --javascript framework...here is example of doing it with the $.axjax() method see http://api.jquery.com/jquery.ajax/ // Send the user data to the server
$.ajax({
method: 'post',
url: 'http://myserverURL/serverprogram.jsp',
data: data,
dataType: 'json',
success: function(data, status) {
console.log('Sent User data!');
},
error: function(error) {
console.log('Error in sending ajax data');
}
});
|
3) Like option #1 you alter something in HTML and the user is clicking on something.
Here the something is a submit button of a form
and the javascript variable values are stored in hidden tags of form.
SCENARIO: the user clicks on the button "Start App Now"
NOTE: your server code should expect the parameter (userid, ****) where *** is the value and get it from the
HTTP Request object your server code has access to. When user clicks on link it will go to the Server program and
pass the facebook userid and return the list of Tweets to the client.
HTML
<from action="http://myserverurl/ServerProgram" method="POST">
<input type="hidden" id="userid" value="****"/>
<input type="Submit" value="Start App Now">
</form
Javascript (in same page as HTML)
<script>
// THIS IS YOUR facebook code ending in storing userid in userid_var javascript variable
//now modify the hidden tag with user id you got above
document.getElementById("userid").value=userid_var;
</script>