This question already has answers here:
How do I pass JavaScript values to Scriptlet in JSP?
(9 answers)
Closed 6 years ago.
I want to pass a java script var to script let in jsp.
i tried many things but not working anything.can anyone tell me a working solution.
i have tried How do I pass JavaScript values to Scriptlet in JSP?
the way this other post stays, is the correct one.
1- create an ajax call (in Javascript) in your webpage to send your desired params to another page.
2- create a page (in jsp) where you request the data sent by the #1.
That's the correct way to go.
#1 Here's an example on how to make an ajax call, you need this on step #1, if your using jquery is simple as:
String data1 = "myData1";
String data2 = "myData2";
String your_jsp_page_to_request = "myJspPage.jsp";
$.post(your_jsp_page_to_request,
{ param1: data1, param2: data2 },
function(data){
alert("Data Loaded: " + data);
}
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#2 Then in your jsp you need to request the data sent, just using:
String data1 = request.getParameter("param1");
String data2 = request.getParameter("param2");
Related
This question already has answers here:
Jquery: How to clear an element before appending new content?
(4 answers)
Closed 2 years ago.
I've recently started using and learning JS and jquery. I'm sending a user email from a form to my PHP page to check if any user exists in DB via ajax, then I get a response from my PHP file if the user exists or not and display the text in a div. It's working fine, but because the page doesn't refresh when the button for sending form data is pressed, ajax just stacks the response from PHP.
I probably need to clear the response every time the button is clicked first and then append the response but I can't find the right command for it :/
JS
function sendnewpass() {
var email = document.getElementById("useremail").value;
$.ajax({
type : "POST",
url : "login.php", //my php page
data : { passemail : email }, //passing the email
success: function(res){ //to do if successful
$("#emailsentmsg").append(res); //the the div that i put the response in
}
});
}
Use empty() like:
$("#emailsentmsg").empty().append(res);
Use the following:
$("#emailsentmsg").html("");
$("#emailsentmsg").append(res);
Or shorter (thanks #Ivar):
$("#emailsentmsg").html(res);
This question already has answers here:
Fetch: POST JSON data
(17 answers)
How to use a link to call JavaScript?
(10 answers)
Closed 3 years ago.
I have a certain JSON object already setted in my HTML code, which via Jinja2 I receive and proceed to perform actions with.
let selectedJson = {{json | safe}}
This is the way I send it in the first place:
#app.route('/pad/analizador222/', methods=['POST', 'GET'])
def analizador222():
littleJSON = {"something" : "something-else"}
mode = 1
return render_template("output.html", json = littleJSON , mode = mode )
Now, after I did some stuff with the object, I need to re-send it back to another "#app.route()" via an HTML LINK, for example:
<a>This is the link I need to click on to send the previously mentioned JSON</a>
Here's the other app.route()
#app.route('/analisis-conversaciones', methods=['POST', 'GET'])
def analisisconversaciones():
return render_template("analysis.html")
I need to do some other stuff that does not matter at the moment with that JSON in "analysis.html"
The question is:
How can I send that JSON object via a common HTML link?
I think POST protocol can help, but in that case I don't know how to use it.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 4 years ago.
I have been trying to pass parameter in a ASP.NET page to another page (in our web site) by redirecting using JavaScript (or jQuery, Ajax, Fetch, etc.) and catch this parameter on Load event of the redirected page via JavaScript. However, I am not sure if I can do this. Any idea?
News Page:
window.location = '/news/details?id=4';
Details Page:
$(function (id) {
console.log('Pass parameter: ' + id);
});
Any help would be appreciated...
On the Details page, use the URL object to get access to the url params, for example:
const url = new URL(window.location.href);
console.log(url.searchParams.get("id"));
You can get the parameters from the url in your details page with the following code:
var url = new URL(window.location);
var param = url.searchParams.get("parameterName");
As #Michael Hurley points out, url is not available in some browsers (namely IE), so if you need to support them you'll have to do some additional work:
exp = /(?:parameterName=)(\w)/
match = str.match(exp)
//match[1] would hold the value of 'parameterName'
This question already has answers here:
Pure JavaScript Send POST Data Without a Form
(13 answers)
Closed 6 years ago.
How can I get Json information from a website API in javascript?
I need to get quotes text from this website http://forismatic.com/en/api/ to put in my quotes generator page.
I think I kinda of get how to great GET requests, but from my understanding this API requires POST requests.
If you have any idea, could you also direct me to the explanation of the code you've written, because I'd also like to understand the underlying logic.
Thanks!
Try this.. Its already working, JSONP
function insertReply(content) {
document.getElementById('output').innerHTML = content;
}
// create script element
var script = document.createElement('script');
// assing src with callback name
script.src = 'http://url.to.json?callback=insertReply';
// insert script to document and load content
document.body.appendChild(script);
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I have been searching the web like crazy for a day straight trying to figure out why my javascript code is not working. I am trying to pass an array from PHP to Javascript using JSON. After that i want to use it in other functions, thus kinda making the array or variable global. But i have been unable to get it to work, here is my code so far:
data = [];
$(document).ready(function() {
$.getJSON('database.php', function(phpdata){
// vad du vill göra här, allt retuneras i data-variabeln
console.log(phpdata);
data[0] = phpdata[0];
console.log(data[0]);
});
console.log(data);
Any ideas?
When $.getJSON is called, the JS engine will step parallely to the next instruction, which here is console.log(data);. This behaivour is caused to $.getJson's asynchronuality.
Since the next step is calculated and executed in the very next millisecond, the webpage was not fully requested and data is still on its initialized value ([]).
What you can do:
Put all instructions that have to do something with your data in another function and call it on the anonymous function function(phpdata) { ... }.
Like this:
function dataLoaded(data) {
// show data in dom elements
// work with data, e.g. check for data["error"] = true or show the username from data["username"]
}
data = [];
$(document).ready(function() {
// Load JSON from server, wait for it and then work with it in dataLoaded(..)
$.getJSON('database.php', function(phpdata){
dataLoaded(phpdata);
});
}
// this is wrong:
// console.log(data);
It is required that database.php will return an JSON array in the JSON data type. See json_encode and header("CONTENT-TYPE: application/json") on the php manual.
Thanks to the comment for saying: failed case sensitive and data type.
What does database.php do? It should probably be outputting a JSON encoded string.
http://us3.php.net/json_encode