How to display json file without calling it directly? - javascript

I've been poking around a great website - http://beta.rallyinteractive.com/ trying to learn something new. Website uses a lot of Ajax in it, and also some interesting javascript - without any additional libraries.
In this single javascript file a found a reference to ajax json get request. You can even easily access the the json file on this url http://beta.rallyinteractive.com/surrounding/studio/ , however I do not understand, why is this data being shown, without the "proper" path to the file.
And by proper I meant "somedomain.com/surrounding/studio/data.json"
Any ideas?

That's just the way they have their site hosting setup on the server and/or within the web application. If you request to the "/surrounding/studio/" folder then it returns the JSON response.

Related

What is the best way to read/write data with javascript?

I want a way to really simply read/write to a json file (as my storage) online. I want to be able to host an app and make axios requests to this to read the data, update it locally and then push the data to the online resource. is there any simple way to do this?
I've used firebase in the passed but this is too long for me to set up now. also http://myjson.com/ only seems to let me do reads. there must be a quick way to host data in a json format that allows reads and writes
I also don't want to use localstorage because other people will be accessing the app
I think it's unfortunate your question is getting down-voted - but that's because the premise of your question is off. Reading/writing a single JSON file is expensive (e.g. timewise) and would be a naive approach (that's okay, we've all been there). What you want is a database. I would suggest starting off with a simple database such as mlab. But even before that, I would suggest you read this article - it gives a good overview of what a CRUD app is.
But if you really want to use a JSON file (again, I would highly NOT recommend this). Then:
You'll need to setup a server
Expose an API endpoint (where you handle GET/POST etc requests) for that server, so clients/people can communicate with said server
Have the server talk to some kind of store (e.g. https://aws.amazon.com/s3/) where the file would live
steps 1. and 2. you'll eventually need to do for any server. But step 3. is why I would NOT recommend using a JSON file. That's because it's very expensive (as noted before) to get the file, change the file, and then put that file back in the store. At that point, you might as well use a database.
Good luck! :)

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.

Is there any way to do an AJAX call to a file above public_html?

I'm making a script that lets my users open the page, vote for our site, and then get a password to some restricted content on the site. However, I plan on storing the password in a file outside public_html so it cannot be read directly from the source code.
Is there any way to do an AJAX call to a file above public_html? I don't want to AJAX to a file inside public_html that will read the file, it'll just defeat the purpose.
Not directly, no. And, frankly, thank goodness for that (since js is executed client-side, and the client should never have access to the web-server above public_html).
You can, however, use Ajax to call a php script inside the web root that has access to documents outside of the web-root. This way you're still keeping the password out of public reach, but still allowing your users to make use of it.
The down-side is that the password might make it to the client-side in the Ajax call (depending on what your Ajax call does). Basically, if JS can get access to the password then so can any interested user.
No, you cannot do that.
The web server does not allow you to do that.
Also, it is highly insecure to expose access to non public_html files on the server.
No, you can't have an AJAX call to a file that's not served by the web server (I'm assuming the file above public_html doesn't have an apache ALIAS or virtual directory setup).
To accomplish what you're trying to do, create a script (php?) on your site that AJAX calls and this script will either:
Read the password file wherever it is on the system (assuming the file has the correct file permissions)
Embed the password within the script itself since the source code of the script can't be retrieved.
No. An AJAX request is simply a request like any other that loads a resource from your server. The only difference is that it exposes the result to javascript on an already loaded page instead of loading a new page. So if an AJAX request can get this secure file, than anyone can.
You could setup a proxy script in some web application programming language to fetch the file from disk and send it along for you. But then it wouldn't be much different from putting the file right in the public directory.
You may need to rethink your approach here.
Why don't you do an AJAX call to some view function on the server that can access the file you need and then return whatever data to the AJAX request?

Pure Javascript app + Amazon S3?

I'm looking to confirm or refute the following:
For what I have read so far it is not possible to write a web application with only javascript -- no server side logic -- served from Amazon S3 that also store data only to S3 if you need to have multiple clients with private data per client.
The issue I see is the Authorization header required for every Ajax call that would force me to put the signature (and my AWS id) right there in the page source for everybody to see.
Is that correct or I misunderstood the docs?
Are there workarounds?
In short, you are correct.
If your AWS key ends-up in any way on the client-side, you are in trouble.
A possible solution is, of course, to have the user specify their AWS key for storing their data.
I'm working on a project that will do something similar to this, mine will have the users use their own S3 which I will store in HTML5 localStorage. It's a bit tricky, but I've got the basics working.
It involves making an Javascript program that replicates itself into S3, gets itself from S3 and then transfers credentials and control into the S3 loaded version.
I'm using the excellent SJCL to do signature generation and jQuery's ajax functionality for the parts I can.
My work simply initializes the S3 side application and does a test PUT/GET sequence to S3. I also rewrote a JQuery postMessage plugin (which StackOverflow won't let me post for lack of rep) for communicating between my frames.
In my case, I'm trying to fit the entire application into a single HTML file so that I don't have to do as much initial transfer into S3, but perhaps there are other ways to work this out.
iBeans offers a way around this without having to write any server side code. There's an S3 iBean (a developer is working on it to be released in the next few days - watch the mulesoft blog for an announcement) and you can access it right from your javascript. The iBean itself runs on a server so you wouldn't need to store your keys in the javascript.

Call Web Services Using Ajax or Silverlight? Which performs best?

I'm building an ASP.NET AJAX application that uses JavaScript to call web services to get its data, and also uses Silverlights Isolated Storage to cache the data on the client machine. Ultimately once the data is downloaded it is passed to JavaScript which displays in on the page using the HTML DOM.
What I'm trying to figure out is, does it make sense for me to make these Web Service calls in Silverlight then pass the data to JavaScript once it's loaded? Also, Silverlight will be saving the data to disk using Isolated Storage whether I call the Web Services with JavaScript or Silverlight. If I call the Web Services with JavaScript, the data will be passed to Silverlight to cache.
I've done some prototyping both ways, and I'm finding the performance to pretty much be the same either way. Also, one of the kickers that is pointing me towards using Silverlight for the whole client-side data access layer, is I need to have timers periodically check for updated data and download it to the cache so the JavaScript can loading when it needs to.
Has anyone done anything similar to this? If so, what are your experiences relating to performance with either the JavaScript or Silverlight method described?
Since Silverlight can handle JSON and XML based services, the format of the response is totally irrelevant. What you must consider, however, is the following:
1) Silverlight is approximately 1000 times faster than JavaScript
2) If your web service is natively SOAP based, Visual Studio can generate a proxy for you, so that you don't need to parse the SOAP message.
3) Silverlight has LINQ to XML and LINQ to JSON, which makes parsing both POX and JSON a breeze.
In a perfect world, I would go with Silverlight for the "engine", and fall back to JavaScript in case Silverlight is not available.
Greetings,
Laurent
Another thing to consider - getting your data in JSON format will be faster than XML and Web Services. JSON becomes a JavaScript object pretty quickly and doesn't have to be parsed like XML does. Personally, I'd go with JavaScript.
article: Speeding Up AJAX with JSON
Since JavaScript isn't multithreaded, I'm finding that using Silverlight to access/cache the data then pass it to JavaScript for display produces much better performance, while refraining from locking/freezing the browser so the user can keep doing stuff while the data loads.
Passing JSON-formatted data is in part faster because unlike an XML SOAP message, it doesn't require a SOAP header or any other miscellaneous info - it's just pure data. Thus, making the total size of the message smaller.

Categories

Resources