-1

Essentially, the question I am asking is similar to this question. However, these Q/A's were of little value to me.

Firstly, there are several people saying that it is impossible to pass a value from a clientside language to a serverside language. However, I know that this is untrue, since I know that it is possible to pass javascritpt (clientside) variables to php (serverside) variables.

Secondly, my need to do this is based on trying to retrieve data from a 3rd party website, to be processed by my program, which will not attempt to return this data to the origional web page. Instead it will be returned to a different location. So, suppose I am retrieving the data required from their webpage using javascript and storing it within a Javascript variable, say var myData = *my required data* and I wish to create a Java program which accepts this data as an input, how would this be done?

Community
  • 1
  • 1
M Smith
  • 430
  • 1
  • 7
  • 19
  • 1
    `Firstly, there are several people saying that it is impossible to pass a value from a clientside language to a serverside language.` well, it _is_ impossible. What you can do is either do it "old style" and submit a form or use an AJAX request to contact the server. Neither of these, however, is JS-to-serverside but rather simple communication. And yes, that's how it works with PHP as well. – VLAZ Nov 06 '16 at 17:56
  • send the data using ajax – Johny Nov 06 '16 at 17:56
  • Are you looking to pass data from your own client-side code to your own server-side code? Or are you trying to scrape data from some other external website? – David Nov 06 '16 at 17:57
  • @David Retrieve data from a a foreign server – M Smith Nov 06 '16 at 18:05
  • @DCooper: Ideally that service provides an API, in which case you should integrate with that. Otherwise, if the data is on a page, you would make an HTTP request from code to get that page and parse out that data using a DOM parser. If that page doesn't initially contain that data but instead has client-side code which generates that data, then what you're looking for is called a "headless browser". You'd need some library in Java which will not only fetch the page but also execute the JavaScript code therein to update the DOM therein. Then you would parse the value from the DOM. – David Nov 06 '16 at 18:07

1 Answers1

0

You can always send client side information to the server. This works in Java as well.

You can make an AJAX request and pass your variable with it.

$.ajax({
    type: 'post',
    url: 'myserverurl.ajx',
    data: 'data=' + variable1 + '&data2=' + variable2
});

You can do this without having to make an async call as well.

Jay
  • 1,089
  • 12
  • 29