Taking an HTML form, passing information to Python Script - javascript

I'm a novice at web programming, and right now, I am trying to write a web interface to python python program.
I'm wanting to fill in a simple form, hit submit, pass data to a python script, have the script pass data back to the same HTML file and display this data on the HTML file.
So, which way do I go about doing this? I have the HTML file done and I have my python program done, but no communication between the two. Should I use post to python using cgi or should I call javascript to create a json object and then pass that in to python and have python pass another json object back to me?

Should I use post to python
Use a form. It is simple and works. The choice between post and get depends on what you are doing with the data.
using cgi
The client submits to the server using HTTP.
How the server communicates with the Python then depends on a number of factors. CGI has the benefits of being simple and easy to set up (but it doesn't scale well).
or should I call javascript to create a json object and then pass that in to python and have python pass another json object back to me?
You could layer something like that on top of the form solution. (Remember to be progressive and unobtrusive). There isn't much benefit in using JSON for the input if you have a flat data structure though. I wouldn't bother with it unless you were dealing with complex data structures on the client.

Related

Convert Pickle Object into JS-readable Code?

I'm pretty new to the Pickle Library and JS. I am writing code in JS to interact with a Python server; when I make a POST to the Python server, I am returned a pickled float. I want to convert the float into a JS-readable object.
The server returns something like pickle.dumps(3.14159,0). When I print this value in Python, I get b'F3.14159\n.' However, the library I'm working with, JPickle, cannot interpret the 'b' in the return (or at least, that's what I'm guessing...when I try to unpickle my response using console.log(jpickle.loads(response)), I get a "unhandled opcode" error). Separate from the POST request, jpickle.loads('F3.14159\n.') gives me 3.14159, but jpickle.loads(b'F3.14159\n.') fails to compile. I'm not sure how to change the response I get from the POST because it is unreadable in JS, so I'm unsure how to go about solving this problem.
I'm open to any solutions that don't involve changing the server-side code (I don't have control over it), including using another library. I've tried JsonPickle with no luck.
Use just JSON, no pickle library required; Python comes with the json module and browser JS can, out of the box, parse json into values.
Serialize your data via something like json.dump and use JSON.parse in the browser to deserialize it.
A little late, but the answers in attached might help:: the code allows Javascript on a browser to traverse and decode compressed pickled objects coming from Python via Ajax.
Is there an already published Javascript solution to traverse Python pickled objects without using Node.js
Make sure that the mime type is allowing you to pass binary numbers.

How to pass variables from Node.JS back end to front end Javascript without exposing what the data contains

This question's answer shows how to do what I'm doing currently:
How to pass variable from back-end to front-end in node.js ejs template
I'm passing the variable into the EJS render call, then using JSON.stringify() to convert it into a format that front-end code can use.
However is there a way to pass the data to the front-end code that doesn't expose what the data actually contains?
For a specific example, say I have an API key for something like MapBox that I'm sending into the EJS render as process.env.MAPBOX_KEY
When I use the JSON.stringify() it exposes that key as plain text for anyone who wants to inspect the page.
I would be grateful if there was a way to get something like that into the front end code in a way that no one can see what it is.

Save large data-set to mySQL table from Javascript without POST?

I have a large-ish amount of server-side data that I need to store in a mySQL table. [I'm a novice, working through the learning curve of javascript & php.]
I'm thinking it's best to stringify the javascript array into a JSON object and send that to a PHP page to save to the database. Once the data's in a PHP array, I know how* to get it into the database; I'm just not sure what's the best way to get it there.
I can't POST (like this example) since the maximum length of a POST string is 2048 characters, and I have maybe 10-20kb of data.
I'd rather not use AJAX or Node.js (like this example) for the sake of simplicity, and since this is a one-off (but both on my list to learn in the future!)
Or, would it be best to create a temp text file to the server with javascript, and then call a PHP page to load & process the data? (Although I can't find examples of how to do that without using POST.)
I understand the difference between server-side & client-side (thanks to this great explanation) but the size limit of POST seems to be my issue?
*Also I'm a little unsure as to when/how it's necessary to encode data (like with this deprecated mysql-real-escape-string example) for storage with {json/posting/DB tables/text}. In this case my data could contain 'single' & "double" quotes (but no foreign characters 国外 वर्ण), which [in my short experience] seem like the only times it will be an issue?
Thanks!
The problem is that Javascript is client side language while PHP is server side language. This means that PHP cannot interact with the user without some HTML, CSS or JavaScript and visa-versa, JavaScript can't interact with server side files without some PHP. Why is this? Since JavaScript is client side the user can edit it as they can see the code while with a PHP script it is all on the server and they are not able to see the code, only what it outputs/prints. So in short you cannot do what you are asking without POST or GET and it is not possible to do this without a server side script such as a PHP script (Python is also very useful if you are thinking of learning more about web backends).
There are numerous example of how to do this that you can find with a simple google search, here is a great example send data to MySQL with AJAX + jQuery + PHP
Hope I could clarify your question.

Want to pass ArrayList contain class objects in javascript

I have a ArrayList which contain objects of a Java Class.I want to pass that list to javascript and want to populate the data within the object in to jsp.
The ArrayList is present in the jsp.Each class object only contain 2 parameter values.Class name is 'Certificate' and it contain 'certId' and 'certName'
jsp:
<%
ArrayList getAllCertificateNamewithId=new ArrayList();
getAllCertificateNamewithId=(ArrayList)mainMap.get("getAllCertificateNamewithIdMap");%>
<input type="button" width="16" height="16" onclick="fetchAndShow('<%=getAllCertificateNamewithId %>')" />
Javascript
function fetchAndShow(certObj){
//How to fetch the list's each object and cast to java class 'Certificate'
}
List contain
[com.dto.Certificate#9b44a1, com.dto.Certificate#1b6b3ac]
Think of it this way: you use Java + JSP as a means to write a text file that you then send to the user's browser, with JSP being a simple template mechanism.
You could theory implement your own method to serialize the List with objects to a String, and then implement a mechanism on the client side to deserialize into whatever data structure you want. For instance you could write your List as JSON.
But now I am inclined to ask: do you really, really, Really, REALLY need to do this ? Exposing server certificates of any kind to the client is a big no-no.
That's not the correct way. Just because of the contents are in the same page, they can't communicate directly.
Java plays on server side and javascript on server side. A client doesn't execute the java code.
To send a Java value to javascript you have to make a server request, So the server process your request and send the results back to Javascript.
Inshort you need to make a server (AJAX) request.
I don't know whether it would be right or not but it's worth a try. Try to use GSON or JACKSON libraries to convert your java object to JSON string. Then I think that you would be able to use that JSON String data in your Javascript code and get the certID and certName wherever you want.

What should be done to JSON data before it's inserted into a page via JavaScript?

I am fetching JSON data from my local server and was wondering what functions I should run my data through before printing it on the page in HTML. Just want to ensure everything is secure and any special characters like quotes are handled properly.
Thanks!
If you are using legal JSON and you are using a real JSON parser, not eval(), then your JSON is safe. It can't contain executable code, only data definitions.
You are certainly free in your client code to take the parsed JSON and run a bunch of sanity checks on the data to make sure it makes sense and passes any specific tests you might want to run on it, but you won't have to worry about code injection if you are using real JSON and a real JSON parser. That is one of the advantages of using JSON - it is a data-only format.
If you're worried about someone hijacking your server and returning bogus data, then you can try to secure the endpoint with https and run any obvious sanity checks in the client.

Categories