I want to set data in HTML local storage using Python (I am using Flask framework).
Do I need to use JavaScript?
You need to use JavaScript.
Python (server-side) knows nothing about JavaScript (client-side) where localStorage resides.
The easiest, however many times not the most efficient, way to approach the problem is
You have a JavaScript JS snippet running in your HTML page
This JS makes an AJAX request to the server-side Flask view. Try jQuery.getJSON()
Flask view returns JSON data
JS code receives the JSON and stores it in the localStorage
Alternative, if the amount of data is few bytes, you can pass it from the server-side to the client-side (JavaScript) using cookies.
Related
I am a newbie in web development. I am in the process of building a food blog website. I want to pursue front-end-web-developer career. I have built my website only using client side languages like javascript, jquery, html and css. I have used json to store the data. my questions are -
1. I now want to make my website live. For that I want my website to be hosted on server. As I am not using any server side scripting, would my data related to website be visible to everyone if they do try to "inspect element" or "view source"?
2. how can I separate Json from client side code/scripting? I think for that I will have to seperate json data from other javascript code and store it into a separate file on a server. But in that case how can I access the Json data? For that I will have to make use of some server side scripting language. I prefer Asp.net(not that I know asp.net but I am familiar with C# hence). I also would have to make changes to my html code at client side to fetch the Json data from server. I am not really aware of this whole thing. Could someone spare some time and let me know how to go about it?
3. Does all the client side and server side code lies in a single asp.net project?
I have searched over internet. Mostly all the material/coding available over internet is mostly in php or python hence I am confused. There no definite guideline that I could find as how to handle the data part only using JSON?
to help you answer your 2nd question
2. how can I separate Json from client side code/scripting?
usually server side is in the controller so your HomeController.cs for instance will have an Index action (public ActionResult Index()) that method will basically be called whenever the browser hits the route Home/Index.cshtml
in order to separate these two, you may fetch or construct your Json in the controller and pass it to the ViewBag, from there you can access it in the view.
using $.ajax will help as well to get the json from the view and update the content of a specific part of your page for instance.
I have a moderately large JSON data file and I want to access it from the JavaScript code on my web page. What is the simplest way to do that? Should this be done on the client side, or is it better to do that on server side?
It can't be done on the client side. Use server to read and print content on page in needed for javascript format. If you want save it you also need to send content to server.
I have a WebSQL database, and am trying to connect to a PHP script, and dump that database into another one on the server.
How do I format the javascript / jquery in order to do that correctly?
I guess I am asking, how do I dump all of my data in my tables into my PHP script, so that it can update and insert the data into another database on the server?
I have no clue where to even begin with this.
The terminology in this question is a little confusing. If you're trying to copy the database from one location to another on the same server, you would only use PHP at most. This could also be done with just your sql administration.
If you are attempting to copy the database to a "Web SQL" database on a client (not a server), I've found a round-a-bout sort of way to accomplish this. I say round-a-bout because in my case I had to copy the database from the server to local client files (aka cross domain). In short:
1) Use PHP to create a json_encode version of your database from the server, using fwrite to create a .json file on the server
2) Using a php proxy to deliver the json data, make a jquery ajax call to the proxy, for the .json file you created
3) Use javascript to store the JSON data into the client's "web sql" database.
I have a series of JSON Objects I want to save locally on my server. I am attempting to avoid any server-side script like PHP as required per demand of whats being built. I know its a security risk, but that in this case is not a particular worry. So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
I should mention I am attempting to avoid ActiveX as I know this is an IE only feature and the software we are developing is planned to be Cross Browser supported
So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
Nope. You will need something running on server side that can receive your JavaScript input and write it to the server.
Internet Explorer's proprietary file writing functionality is for writing local (client-side) files only.
You can read a file using ajax, but without a server side language you cannot write a file to the server.
https://developer.mozilla.org/en/ajax
No. Javascript runs on the client. You need server-side code to access the server's file system.
Client-side JavaScript can only send data to a server, there's no way for it to tell the server what to do with the data.
To save data to a file or db on a server, you'll require a server-side script of some sort (could be server-side JS with Node.js). If all you need is persistent data, you could store some JSON strings in localStorage or in cookies as needed. They wouldn't be shareable that way though.
Yes, you can use AJAX requests in JavaScript without using jQuery. However, jQuery will save you an ungodly amount of time and cross-browser testing.
But, as others have already said, you can't write server files without server code.
Is it possible to add a JavaScript variable in an SQL Server database database (run an SQL query or stored procedure)? If so, how?
With JavaScript only, no you cannot (unless you're using Node.js of course).
The point is, you'll need some server-side code to interact with your database. You can use JavaScript in the browser to make an Ajax call to a server-side script (PHP, Ruby, Python, ASP.NET, Node.js, etc.) that performs the interaction with the database.
You can not access the database from JavaScript. You need to send your data to PHP, Ruby on Rails or ASP.NET or whatever you are using to implement the back end.
You can use Ajax to post to a web service of some sort (or any other server side script) to pass the JavaScript value. The web service would access the database and save it...
Set value of variable in the hidden field and get value of that field at server side when submit form and save it into the database....
You can easily access a database from JavaScript in Node.js.
If you're doing it from the browser, you'll most definitely need some kind of mid-tier service which translates HTTP queries to the relevant database driver (you can write that in any language you want - PHP, JavaScript, ASP.NET, etc.).