Get Json data with POST request from website API [duplicate] - javascript

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);

Related

How to send JSON from HTML link to Python Flask? [duplicate]

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.

Redirect to another page and pass parameter via JavaScript [duplicate]

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'

render_template in Flask on JS click event [duplicate]

This question already has answers here:
replace div content using ajax and jquery
(4 answers)
How to manage a redirect request after a jQuery Ajax call
(34 answers)
Closed 5 years ago.
I am trying to call a function in Flask by clicking a button. When the button is clicked, the following script runs.
JS
$("#load_btn").click(function() {
$.get("/callback");
}
Flask
#app.route('/callback')
def callback():
print('called')
... # code here that needs to run before rendering callback
return render_template('callback.html')
The problem here is, called is printed, and GET /callback HTTP/1.1 200 is logged as well, but callback.html is not rendered - the page stays the same. I'm guessing this is probably not the best approach to solving this problem, but if so what is the recommended approach?
$.get just sends an HTTP request to your server, but doesn't actually do anything anything with the data (in your case, the rendered template) it gets back from the server.
Try something like this:
$("#load_btn").click(function() {
$("html").load("/callback");
}
Or if the template you render doesn't contain any <head> tags, only body content, you can do
$("#load_btn").click(function() {
$("body").load("/callback");
}
Or, you can exchange "body" for a specific element to only replace a specific part of the page.
If you want the user to be redirected, do this:
$("#load_btn").click(function() {
window.location = "/callback";
}

passing variable from java script to jsp script let [duplicate]

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");

How to provide javascript variable as a parameter to asp.net method [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
For this following code:
var idArray = ["10","20","30"];
$.each(idArray, function(i, value){
if(<%= GeneralHelper.GetItemUrlByItemId(value)%>){ //I cannot use the variable value
alert('record found');
}
});
Code for my class:
public sealed class HelperClass{
public static bool HelperFunction(string Id){
// check some data in database against the id
// return true if record found
}
}
The problem:
Server-side code is evaluated on the server, before anything is transmitted to the client.
Client side code (JavaScript) is evaluated in the browser, after the server has finished transmitting the document.
After understanding the above statements, you'll see that your current example is technically impossible.
What you will need to do (if your goal is to prevent a page refresh) is send the new data to a script on the server using AJAX. The script will process the data and respond to the browser.

Categories