So, I'm starting to learn JSON handling in javascript, I have an JSON file like this one:
[{"name":"Krzysztof Kowalski", "title":"Na Zdrowie", "author":"Jan Kochanowski"}]
And I'm trying to print it into a website, if I'm not wrong this:
<html>
<head>
<script type="text/javascript" src="data.json"></script>
<script>
function updateUser() {
var configs = JSON.parse(data);
document.getElementById('txt').innerHTML = configs[0].name;
var t = setTimeout(startTime, 500);
}
</script>
</head>
<body onload="updateUser()">
<div id='txt'>
<p>Test text</p>
</div>
</body>
</html>
Should print the "name" field from the first object in JSON, although I get an error like this:
Uncaught ReferenceError: data is not defined
Does anyone know the solution to this problem?
EDIT: changed
document.getElementById('txt').innerHTML = data[0].name;
to
document.getElementById('txt').innerHTML = configs[0].name;
For it to work, your JSON file must rather look like this:
data = '[{"name":"Krzysztof Kowalski", "title":"Na Zdrowie", "author":"Jan Kochanowski"}]'
If you prefer to go up with a conventional JSON file, http://api.jquery.com/jQuery.getJSON/ should help in that more normal approach.
Related
I'm trying to take an image from the page and preprocess it before passing it to the model (which is based on VGG19 architecture). When I try to use the provided vgg19 preprocessing application, I receive an error from Java : "Uncaught (in promise) TypeError: Cannot read property 'applications' of undefined"
Am I calling the vgg19 function appropriately? I tried different versions of tf.js as well, to no avail.
<html>
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs#2.0.0/dist/tf.min.js"> </script>
<head></head>
<body>
<p>Architectural Style Classifier 1.3</p>
<img id="imgcanvas" height="300" src='testimage1.jpg'></img>
</body>
<script type="text/javascript">
async function run(){
const image = tf.browser.fromPixels(imgcanvas);
const batchedImage = tf.keras.applications.vgg19.preprocess_input(image);
const MODEL_URL = 'web_model/model.json';
const model = await tf.loadGraphModel(MODEL_URL);
const result = model.predict(batchedImage);
result.print();
}
run();
</script>
</html>
The webpage if you'd like to try it yourself:
https://areddy831.github.io/
Or the github repository with the html file: https://github.com/areddy831/areddy831.github.io
Solved: I was trying to use a python module - vgg19 pre-processing is not available in tensorflow.js
I have the following html code (index.html):
<!DOCTYPE html>
<html>
<head>
<script>
function addParagraphText()
{
var arg1=2
var arg2=34
var arg3="john"
var myResult = runMe.py(arg1, arg2, arg3)
document.getElementById("para").innerHTML = myResult
}
</script>
</head>
<body>
<button onclick="addParagraphText();">Click me</button>
<p id="para"></p>
</html>
and the following python file runMe.py:
import sys
myArg1 = sys.argv[1]
myArg2 = sys.argv[2]
myArg3 = sys.argv[3]
# some long calculations using various libraries and modules
myOutput = '''<table style=\"border:solid;\"><tr><td> bla </td><td> lol bla <button>myButton</button> </td></tr><tr><td> blabla </td><td> blablabla </td></tr></table>'''
return myOutput
Obviously it doesn't work. What would be the easiest and quickest way to do it? I have a very long Python script that does some calculations and outputs a rather long string in html format which I would like to present after user clicks on the button. As of now, I do not need a state of the art approach, I am rather looking for a fast and easy solution.
In short: JavaScript in a html page executed by a browser cannot start the python interpreter and cannot run python scripts.
Browser security will prevent this.
You'll have to use something like XHR and cgi like in
this question. The first answer seems to be a complete example.
Otherwise try wsgi python.
I'm using the current json structure of this:
jsonp = {"game":[
{"id":"1","gameImage":"qqq.jpg"}
],
"game":[
{"id":"2","gameImage":"hhh.jpg"}
]
}
I'm trying to just get back all the gameImage values. I tried the following but it just won't work. Any ideas?
<html>
<head></head>
<body>
<script type="text/javascript" language="javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript" language="javascript" src="gameData.json"></script>
<script>
$(document).ready(function() {
var obj = $.parseJSON(jsonp);
$.each(obj, function() {
alert(this['gameImage']);
});
});
</script>
</body>
</html>
Assuming the first code you show is in the file (incorrectly) named "gameData.json", what you're trying to parse isn't JSON (or JSONP), it's plain JavaScript.
So don't parse it.
Change
var obj = $.parseJSON(jsonp);
to
var obj = jsonp;
Notes :
JSON is a text based data interchange format. Your confusion might come from the many young developers using non-sensical expressions like "JSON object"...
You should avoid naming jsonp a variable holding a plain JavaScript object.
If you prefer to load a JSON file instead of executing a JavaScript file, then
remove the "jsonp = part to make it a real JSON file
remove the script element (as it's not JavaScript anymore)
load the JSON file using ajax
Here's how the JavaScript would be like :
$(document).ready(function() {
$.getJSON("gameData.json", function(obj){
$.each(obj, function() {
alert(this['gameImage']);
});
});
});
or, if you can afford not supporting IE8 :
$(document).ready(function() {
$.getJSON("gameData.json", function(arr){
arr.forEach(function(item){
console.log(item.gameImage); // yes, prefer the console over alert
});
});
});
Here is my code for a page...
<html>
<head>
<script src="jquery-1.7.2.js"></script>
</head>
<body>
<script>
var url = "https://natiweb-natiweb.rhcloud.com/game.php";
$.getJSON(url,function(data){
$.each(data,function(i,user){
alert("inside json");
alert(user.appname);
});
}
);
</script>
</body>
</html>
I'm unable to get data from server.... alert does not gets popped up
it works fine if I write the script code inside a js file. but i need to dynamically populate a page, hence I need to write this query inside body.
I added alert inside the json function. even that is not getting popped. I think json isn't getting executed
Try This.....
var url = "https://natiweb-natiweb.rhcloud.com/game.php";
$.getJSON(url,function(data){
$.each(data,function(i,user){
alert(user.appname[i]);
});
}
);
try this one,Becoz you are getting array in that array your json is present
alert(user[0].appname);
Try to auto-execute your code, that's what the example says on the jquery doc.
<script>
(function() {
var url = "https://natiweb-natiweb.rhcloud.com/game.php";
$.getJSON(url,function(data){
$.each(data,function(i,user){
alert("inside json");
alert(user.appname);
});
}
);
})();
</script>
I am using the ajaxslt javascript library.(http://code.google.com/p/ajaxslt/) I am trying to get the node using XPATH
My XML is as follows:
<page>
<message>
Hello World.
</message>
</page>
I am trying to use //page so that I can all nodes below page i.e. message node. When i try to print the same. I am getting only Hello World as output.
Following is the code snippet i used.
<script src="./js/xpath.js" language="JavaScript"></script>
<script src="./js/xpath_script.js" language="JavaScript"></script>
<script type="text/javascript">
function showMessage(){
var xml = document.getElementById('xml');
var ctx = new ExprContext(xmlParse(xml.value));
var expr = xpathParse("//page");
var result = expr.evaluate(ctx);
alert("res:"+result.stringValue());
}
Can anybody tell me what i am doing wrong here?
Thanks in advance.
Saravanan K
You must use:
//page/message
this selects in general more than one message element. You need to iterate through the returned node-list and produce the string value of each selected message element.