Importing external json files to a javascript script client-side without external libraries

I'm a bit new to javascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?

Answers


You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.

EDIT:

If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.


Yes there is. You can use the "document.write" to add scripts to the DOM at runtime: in your case:

document.write('<script ...></script>');

Basically you are adding the script tag to the dom that will request the new file. However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:

var x = { //json object };

Need Your Help

@ExceptionHandler only working with Exception.class

java spring spring-mvc

I'm using the @ExceptionHandler annotation on one of my controllers, but if I use any Exception type other than Exception.class, the page returns the message

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.