Passing Value from Java to JavaScript

How to pass variable values from java to javascript

$.ajax({
url: "YourSeveletPathName",
type: 'GET',
success: function(response) {
//console.log(response);
//map your data here - to series parameter in your highchart method.
},
error: function(error) {
errorFunction(error, parameter);
}
});

How to pass variable from java to javascript?

You need to use @ResponseBody and inside the ajax use the success callback to get the value of success of ajax.

DataTypeOfReturn is the type of the data which you want to return & it can be int/String

function edit(id) {
jQuery.ajax({
type: "GET",
url: "getId",
data: "id= " + id,
datatype: "text",
success: function(data) {
console.log(data)
}
});
var type = <%= ((String)request.getAttribute("myType"))%>;
console.log("type is " + type);
}



@RequestMapping("/getId")
public @ResponseBody DataTypeOfReturn getId(
@RequestParam int id, HttpServletRequest request) {
int idBook = id; // add data type here to avoid java error
System.out.println("get id book " + id);
String type = BookDao.getTypeCategory(id);
request.setAttribute("myType", type);
System.out.println("request attribute" + request.getAttribute("myType"));
return theValue; // value which you want to return
}

Pass variables from java to javascript

Would recommend following:

  1. Install apached tomcat
    http://tomcat.apache.org/tomcat-6.0-doc/appdev/

  2. Try Hello World example included in Apached Tomcat
    http://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/

You would find values in variables shown in GUI.

Next Step assigning java variables to javascript variables, use following syntax

<script type="text/javascript">
var jsVariable =<%javaVariable%>;
</script>

Hoping this helps.

Cheers !!

Passing value from java to Javascript

Could you do it thru WebView.evaluateJavascript()?

https://developer.android.com/reference/android/webkit/WebView.html#evaluateJavascript(java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.String%3E)

So with that you could send simple CustomEvent to document in WebView

webView.evaluateJavascript("document.dispatchEvent(new Event('printer_error', { details: "No printer found!" }));");

and in JavaScript you can hook listener for your custom event to react.

document.addEventListener('printer_error', e => alert(e.details));

Didn't test this so might be that at least evaluateJavascript() needs callback function.

How to pass data from Java to a JavaScript web application?

If you assume that the web page is always going to be viewed on the same machine that is running the JavaFX application you could always have it export the file via HTTP (using Jetty or similar), then have the web page load the data using a URL such as "http://localhost:port/fileContainingData".

Pass jsp variable to javascript

Another way to do it:

<input type="hidden" value="${s.status}" id="status">
<script>
var status = document.getElementById('status').value;
</script>

how to pass a java variable to javascript

use inline script for example:

 <script type="text/javascript" >

var d1 = [];
for (var i = 0; i < 14; i += 0.5) {
d1.push([i, Math.sin(i)]);
}

var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

// A null signifies separate line segments

var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];

$.plot("#placeholder", [ d1, <%=otp1%>, d3 ]);

// Add the Flot version string to the footer

$("#footer").prepend("Flot " + $.plot.version + " – ");

</script>


Related Topics



Leave a reply



Submit