Accessing JSON data from JavaScript on a web page - javascript

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.

Related

how to store json file at server using asp.net as server side language and how to access json data using javascript, html and css?

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.

Write to server: using text file with HTML5 Jquery/Javascript

I was wondering if it is possible to write to a server and storing it in a text file using Javascript/Jquery, HTML5. The purpose is, I am making a javascript/jquery game and I wanted to store the scores, without using any server-side language (php, asp, etc.)
Thank you so much for the help, your time is much appreciated.
HTML, JS, CSS, all that is client-side. Meaning it runs in the browser and that's that.
PHP & ASP are server-side, meaning the code gets executed on the server and the result gets sent to the client.
You need a method of communication between the client and server. You can send requests using JS (AJAX) but you'll need something on the server to receive and do something with them. For that you need some server-side code.
So no, you can't update data on a server using only client-side technologies. You can, however, use client-side scripts to communicate with a server and tell the server-side code to update data.

Compress text data in javascript and send it via AJAX

I develop web application that supposed to communicate with server using jQuery/AJAX/JSON. Just was curious, is it possible to compress somehow text data before send it to server and extract it there? Are there any already implemented JavaScript compressors?
In other words, how to implement next scenario:
Compress text data on client side using JavaScript
Send compressed data to the server
Decompress data on the server side
any ideas?
Yes, any compression algorithm written in JS will do, or look into setting compression settings in the HTTP headers of the requests you're sending.

How do you save an image on webapge to the server with javascript?

I have a image with id='canvasaImg' that sits on my webpage. I was wondering if it was possible to save this image to the server with javascript or if it has to be done with php.
This example also would help Save image to server from a URL
Saving image to server with php
http://www.sitepoint.com/forums/showthread.php?403537-Save-picture-from-website-to-a-server
Short answer - You can't do this with JavaScript alone - you'll have to use some serverside code too.
Long answer - JavaScript is a client side technology. It has no control over the server. What you might be able to do is execute an AJAX call to your server containing the path to the image and then with PHP (or any other server side language) you could use file_get_contents() to copy the image to your server.
No, you can't.
Yes, you have to use PHP or another server-side language.
However, there is another way to interpret your question which would give the answer:
Yes, you can save an image to the server using JavaScript with Node.js (Note that Node.js is a web server which uses JavaScript)

Calling client javascript with params from server side in ASP.net

I have an ASP.net webpage, that periodically (once in a minute) makes a call to my WCF REST service. My REST service responses some XML data. After getting it I make some further operations on that on server side in my ASP page.Note, this post data process in ASP is required, I can't avoid it. I know my life would be easier without this step, but I must do it.
After I'd like to pass this data in XML format to a client side javascript, that can parse it and show infos to the user based on this data. How can make this call from server side? What is the best pattern/practice to do it?
.net4/VS2010
if you want to call a function that already exists, that will load your data to the screen, you can tell the server to return your data and then ajax will grab that data and call a callback function.
if you are not using ajax, you can reload the whole page with the new data.
HTTP is not designed to push data from the server to the client. I'm not really familiar with ASP but usually you have the following possibilities to "push" data to a client javascript application via HTTP:
page reload via meta refresh (which doesn't actually push data;) )
periodically polling an "job queue" URL using javascript
comet (see http://en.wikipedia.org/wiki/Comet_(programming)) for an overview)
Web Sockets (which actually pushes data to the client but is only supported by newer browsers)
I've been using atmosphere (http://atmosphere.java.net/) which works pretty well in java application containers, which provides an abstraction layer over the underlying technology. I don't know if there is something similar out there in the ASP-world.
cheers
Martin
Tom, in that case just do the following

Categories

Resources