I have the following code:
<script type="text/javascript">
$(document).ready(function(){
$("form.loader").on('submit',function(e){
e.preventDefault();
$.ajax({
url: "data.json",
type: "POST",
cache: false,
success: function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
});
});
});
</script>
<form class="loader" method="POST" enctype="multipart/form-data">
<input type="file" name="data">
<button type="submit">Cargar datos</button>
</form>
So what I want is to be able to load the JSON from an input file and store the entire content of the JSON in a variable. To apply modifications to it and again save the file :)
But for now I would like to just open the file to manipulate it.
In reply to @json
Using load is great for displaying a file as such, but if I wanted to store the contents in a variable how should I do it?
And for some comments...
Well, little use GET. I usually always use POST. If I must use GET, I would like to know what is the reason :)
Well, if what you want is to show the content of a txt in a file, it would
div
be more or less like this.you put the
div
where you want it to be displayed and the button to execute the action.I hope to be helpful.
Depending on which side the file is on, you could proceed in one of two ways: reading the file from the client device (explained in A), or from the server (explained in B).
Your question:
I will answer it in (C).
A. IF THE FILE IS ON THE CLIENT SIDE
You can do it using FileReader . The client must select the file by clicking the button:
Note:
This method is supported by:
B. IF THE FILE IS ON THE SERVER SIDE
You can do it with jQuery. In the example, the url of the file is entered in the input and when the button is pressed, the file is requested from the server through the Ajax get method . If the response is successful, the file is displayed in a div.
C. WHICH METHOD TO USE?
In the first case none, since the server does not intervene at all. In the second case the GET method.
Source: http://www.cs.tut.fi/~jkorpela/forms/methods.html
You could solve it using:
API FileReader
Which allows dynamically reading a file entered by the user .JSON.parse
which allows us to transform a stringJSON
to an object.Example:
Once you upload the file, you can save the file in a SessionStorage or a LocalStorage, both are very good, since it allows you to store data on the client side with a maximum of 5mb approx. The difference between one and the other is that Session is removed if you close the browser, the other is not.
This is invoked as follows
You pass the "variableJson" to the session or local variable with the name "variable". This is saved on the pc as a temporary file. If you need to rescue it, it should be done as follows.
and if you need to delete, just
I hope it helps you.
And post is always better, since get reveals the parameters that you send to your server through the url. And it is considered as "vulnerable" if I need to attack your site, since that way I begin to understand how your server works and how I can drop it through this block.
Good luck with whatever you're working on :D
Assuming that the file is on the server, we would load it with jquery 's .load() and store the content in a textarea so that you can edit it and once placed, the comments or changes you want to make to the file, you capture its content and you send by $.post to a php script to save the changes.
Form:
in the script.php to add the content to the end of the file: