How to mix python and javascript in the server side - javascript

I'm creating an application that needs to pull certain data to show it to the user. This data is pulled via a JavaScript function that ideally I'd like to have on the server side, not on the source code of the page.
The flow should be:
User chooses 1 parameter in the website and clicks ok
Send a POST request to Django with that parameter
Django's view uses that parameter to pull another 4 parameters from the Django database
Somehow use this 4 parameters in the JavaScript function to get some timeseries data
Pass this data from JavaScript to the view, and from the view update the browser's template without the user refreshing the page
How can I pass the 4 parameters from Python to the JS function, and then the result of the JS function back to Python?
The reason I need to use JavaScript and not Python for retrieving that data is because JS has a specific library that would make my life so much easier.

Related

Re-run PHP rss feed

I couldn't really find anything online for what I was looking for.
Currently, I have some php code that grabs news feeds and every time the loop runs through, it stores it in an array slot {0,1,2} etc. The interesting part is, I don't know how to refresh the php rss grab function without refreshing the page.
Essentially I have index.php, and with code inside, and I'd like to re-run the php script in those arrows <> through javascript.
I know in javascript you can assign script names and call them in html, that's essential what I want to do but for php, is it possible?
Currently your RSS fetching logic is intertwined with your presentation logic. You need to separate them so the RSS logic can be called independently.
Consider a structure like this:
rss.php - script with logic to fetch RSS feed and package it up as PHP array
index.php - require(s) rss.php and wraps the results in HTML
api.php - another script which require(s) rss.php, but responds with JSON data that can be consumed by Javascript
Now you can present the RSS data when the page loads and then periodically call api.php via Javascript to update the results.
You could also forgo calling rss.php from index.php, merely have it present an HTML skeleton with the javascript and when the page loads, have the javascript call api.php right away to build the initial list. That way you don't have the presentation logic in two places.
PHP is a server-side language, meaning your script will be run by the server before it is returned to your client (the browser).
What you can do if you want to continuously get new data from your script is call your server periodically with JavaScript using an AJAX call and display that to your user.
See this and this.

How to make a call from javascript to jsp and from jsp to another url and get the values?

I need to create a JSP in which it will send a request to an external system and get some values. These values need to be populated in my frontend javascript application. The logic is like the javascript will call the JSP and the JSP will get the values from an external system (some other server) and these values will be taken back to text boxes in javascript page.

Can we call JSF controller method from jquery ajax? [duplicate]

I am doing a POST-request using jQuery which seems to succeed. But how can I work with that on server side and modify the response?
Do I need another servlet because the Faces Servlet is just not designed to deal with this?
$.ajax({type:'POST', data:{"status":status}, success: function(response) {
alert("Qapla'");
}});
It is used for the following process:
user inputs address and hits commandButton which invokes JS
JS retrieves geodata using google maps and sends it to server (which I am considering to use the above code for)
the servers responds sending some close places from database
JS retrieves exact distances using google maps again and sends them to server
server redirects client to next page with results
There is one case where a failing validation for the used inputText might be needed: At point 2 the server rates the geodata as not valid.
If sending the ajax POST by usual JSF means (UICommand component, jsf.ajax.request(), etc, in flavor of <h:commandButton>, <p:remoteCommand>, <o:commandScript>, etc) is really not an option for some reason left unspecified in your question, then you'd indeed better create a separate servlet or even JAX-RS or JAX-WS webservice listening on those requests and returning e.g. XML, JSON, etc. JSF is a HTML form based MVC framework not a web service framework.
You only need to take into account that you deal properly with JSF view state when you manipulate the HTML representation of JSF components afterwards. E.g. when you use custom JS/ajax to enable a disabled HTML button as generated by <h:commandButton> without involving/notifying JSF, then it won't appear as enabled in JSF component state and its action would never be invoked.
See also:
How to use Servlets and Ajax?
How to generate JSON response from JSF?
How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?
What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?

How to pass a value to MySQL from an infinitely looping Javascript function?

I'm trying to write a simple web app that will read from a 1D barcode and insert the value to a MySQL database.
Ideally this website will access to a camera and just scan the barcodes, that are shown to it. There will be no further user interaction.
I have achieved scanning the barcodes and extracting the information in Javascript using ZXing. Now my research has shown me that you can't just insert a php inside javascript. So I must stop the infinite loop of the function and pass data to php, where it can be inserted to MySQL. However after I return from the function and update the database, I need to refresh the webpage to scan a new barcode.
The problem is here I don't want to refresh the webpage because the browser, that runs the webpage won't have any mouse/keyboard(user interaction). How can I call a javascript function infinite times without refreshing my browser?
After scaning, send data to php by ajax (try some javascript framework like jQuery or others...)
By javascript, you can refresh page in oncomplete state of ajax request without any keybord or mouse action.
I think your best chance is to take a look to javascript Ajax calls.
In client side
- The infinite loop call to a function that handles de ajax call. That ajax call should sent a GET o POST to a php page.
- You don't need to refresh the page. If you need to return some data, do it in the ajax response function.
In the server side
The .php handle the insertion of data to MySQL.
Recomendation:
Use jQuery, a javascript library: https://jquery.com/
Take a look:
Using Jquery Ajax to retrieve data from Mysql

Flow of Jsp -> Javascript webpage (Ajax?)

I am making a website but I get a bit lost programming dynamic sites.
The user needs to enter x (inside a textbox), click submit, process in java (serverside) and present outcome as a report to the user (using javascript).
I'm at the moment using JSP to process the users input but now I need to pass the JSON code into the javascript. The javascript requires JSON data.
At the moment I have JSP which returns the necessary JSON code and the Javascript which works with hardcoded JSON code. I need to somehow store the returned JSON (from the JSP) in a variable and pass it to the Javascript. I have a vague understanding of AJAX - i'm just unsure if this is possible and how to link it all together.
Thank you.
Your JSP "page" can simply generate JSON directly. There's no reason the output from JSP has to be HTML. From the client, therefore, you POST to the server, the JSP runs, and the result (pure JSON from your JSP) is sent back to the client as the ajax response.
Sounds like the perfect place for AJAX - you can submit the request with javascript, and when it comes back process it further with javascript. Unless you want the page to refresh.
If you are using jQuery, you can look here to see how to implement it, should be relatively painless: http://api.jquery.com/jQuery.post/

Categories

Resources