My python code creates HTML files. I work on Anaconda Enterprise and my problem is that I want to read an external json file in javascript encapsulated in HTML
message = """
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="sample.json"></script>
</head>
<body>
<p id="GFG_DOWN" style="color: green; font-size: 20px; font-weight: bold;"></p>
<script>
function pointe_vers_doc(clicked) {
var test = document.getElementById("GFG_DOWN");
var mydata = JSON.parse(data);
test.innerHTML = "Value of my json = "+mydata[0];
}
</script>
</body>
</html>
"""
Here is the external json (sample.json) I want to read :
data='[["first", "second"], [], ["fourth"], [], [], ["seventh"]]'
Locally it works but on the server (AE) I think it does not find the path to sample.json
I tried to change the path several times by testing but it doesn't work.
Is there another way to proceed ?
Related
I'm using the JSONEditor (https://github.com/josdejong/jsoneditor) to load a json file, make changes and save the file. This works great but it only saves the JSON file to the folder specified according to your browser settings. Here's the demo code (https://github.com/josdejong/jsoneditor/blob/master/examples/04_load_and_save.html):
<!DOCTYPE HTML>
<html>
<head>
<title>JSONEditor | Load and save</title>
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
<script src="../dist/jsoneditor.js"></script>
<script src="https://bgrins.github.io/filereader.js/filereader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<style>
html, body {
font: 11pt sans-serif;
}
#jsoneditor {
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<h1>Load and save JSON documents</h1>
<p>
This examples uses HTML5 to load/save local files.
Powered by FileReader.js and
FileSaver.js.<br>
Only supported on modern browsers (Chrome, FireFox, IE10+, Safari 6.1+, Opera 15+).
</p>
<p>
Load a JSON document: <input type="file" id="loadDocument" value="Load"/>
</p>
<p>
Save a JSON document: <input type="button" id="saveDocument" value="Save" />
</p>
<div id="jsoneditor"></div>
<script>
// create the editor
var editor = new JSONEditor(document.getElementById('jsoneditor'));
// Load a JSON document
FileReaderJS.setupInput(document.getElementById('loadDocument'), {
readAsDefault: 'Text',
on: {
load: function (event, file) {
editor.setText(event.target.result);
}
}
});
// Save a JSON document
document.getElementById('saveDocument').onclick = function () {
// Save Dialog
fname = window.prompt("Save as...");
// Check json extension in file name
if(fname.indexOf(".")==-1){
fname = fname + ".json";
}else{
if(fname.split('.').pop().toLowerCase() == "json"){
// Nothing to do
}else{
fname = fname.split('.')[0] + ".json";
}
}
var blob = new Blob([editor.getText()], {type: 'application/json;charset=utf-8'});
saveAs(blob, fname);
};
</script>
</body>
</html>
I want to be able to save the file to the web server. Is there any way for me to save the edited JSON file to the web server? I searched and tried to integrate this library with JSONEditor but no joy:
https://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax
I'm not restricted to ajax so I'll consider anything that works!
Thanks for your advice.
John
UPDATED: Here's the controller code chunk.
// POST api/values
public async void Post()
{
string json = await Request.Content.ReadAsStringAsync();
File.WriteAllText(
HttpContext.Current.Server.MapPath("~\\App_Data\\somefile.json"),
json
);
}
I tested this using Postman and it works. What I can't seem to do for the life of me is to now send the edited JSON file to the controller. Here's the modified HTML page where I try unsuccessfully to send the json. For brevity, I'll just add the extra/edited code code:
<form id="form1" method="post" enctype="text/plain" action="http://localhost:1651/api/values">
<input type="file" name="json" id="loadDocument" value="Load"/>
<input type="submit" value="Save" />
</form>
Edited javascript where I try to return the edited json to the form:
document.getElementById('saveDocument').onclick = function () {
return editor.getText();
};
Please help! How do I send the json to the controller?
I am still a beginner in JavaScript. I have written a code with two nested for-loops. It achieves the result but that is too large to be copied in Android. It copies part but not all data.
How can I export my data as .csv file or .txt file i.e. a button in HTML file, I click on it then the data is exported as .csv?
My HTML code:
<html>
<head>
<title>Page Title</title>
</head>
<body>
<button onclick="convert()"> Convert </button>
</body>
</html>
My JavaScript code:
function convert(){
var lon,lat;
for(lat = 52 + (2226/3600) ; lat <= 52 + (2425/3600) ; lat=lat+(1/3600))
{
for(lon = 9 + (3539/3600) ; lon<= 10 + (215/3600) ; lon=lon+(1/3600))
{ document.write("W,"+lat+","+lon+"<br>") }
}
};
My data are browsed fully and without problem in webpage HTML as followed after clicking convert button:
W,52.61833333333333,9.983055555555556
W,52.61833333333333,9.983333333333333
W,52.61833333333333,9.98361111111111
W,52.61833333333333,9.983888888888886
W,52.61833333333333,9.984166666666663 ..........
W,52.61833333333333,9.98444444444444
Hopefully I can export all these data so as .csv or .txt file.
I want to write a Javascript in a standard location and connect this file to a page html.
Write: (using Swift 1.2)
let path = NSTemporaryDirectory() + "foo.js"
var error: NSError?
text = "12345"
text.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: &error)
html:
<html>
<head>
<script src="path/foo.js"></script>
</head>
<body>
<p> Use Foo </p>
</body>
Which path i can use?
Do I need to place the files in a local-server or any server to get the results?
index.html:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta char-set = "UTF-8">
<title>AngularJS | Passing Data between Different Scopes</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script src = "controller.js"></script>
</head>
<body ng-app = "mainApp">
<div ng-controller = "app">
<li ng-repeat = "i in myRange">
{{i}} <br/>
</li>
</div>
</body>
</html>
controller.js:
var app = angular.module('mainApp', []);
app.controller('app', function($scope) {
var range = 10;
var myRange = [];
for (i = 0; i<range; i++)
{
myRange.push(i);
}
$scope.myRange = myRange;
});
When i'm running using localhost it gets the result i wanted. But when I run it without using any server or local-server it shows only the html page, not returning data from controller.js file. As i know this must work without using of any local-server. what's the wrong here?
Edit:
When i run the html file without using local-server the output is as follows.
As #csharpfolk suggested, the problem is with loading angular.js library use 'https://' instead of '//'. If you use '//' browser will try to load using 'file://' protocol.
I am new with json and ajax. I would to ask for assistance on how to Retrieve JSON value using AJAX.
Here is my json file named as list.js
{
"loginid" : "Wafiqa",
"password": "123"
}
and here is my html fille named as ajaxTest.html
<body>
<p> Username: </p>
<div id="uname"></div>
<p> Password: </p>
<div id="pword"></div>
</body>
What I want to do is the value from username in json file will be displayed at uname div and the password will display at pword div. How can I do that?
Once you're just starting to learn JavaScript, it would be easier for you to just put your JSON data in a variable.
var jsonData = {
"loginid" : "Wafiqa",
"password": "123"
};
and then you insert it in the DOM with something like
document.getElementById(uname).innerHTML = jsonData.loginid;
document.getElementById(pword).innerHTML = jsonData.password;
If you want to use the json in another file you should serve it as a static asset or use some server-side code to render it.
You cannot access a local file. You will need the json file to be served from a server. You can run your code form a web server.
You can use the jquery#getJSON function for this.
For sample, in your html head section have
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.getJSON demo</title>
<style>
img {
height: 100px;
float: left;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="images"></div>
<script>
(function() {
$.getJSON( 'http://date.jsontest.com/', {
format: "json"
})
.done(function( data ) {
alert(data.date);
});
})();
</script>
</body>
</html>