-2
I’m with a project where I have an HTML input, where I send the link to an image of a car, goes to an AI that searches for an API. So far so good, the result comes back right on the console. The problem is that I wanted to know how to take the result of this JSON, to HTML. Follow the code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="data"></div>
<input type="text" name="link" id="link">
<input type="submit" name="button" onclick="clicouBotao()">
<script src="https://algorithmia.com/v1/clients/js/algorithmia-0.2.1.js" type="text/javascript"></script>
<script>
function clicouBotao(){
var input = document.getElementById('link').value;
Algorithmia.client("simwTGCAoebHnAZc7rgS6NkkxxV1")
.algo("LgoBE/CarMakeandModelRecognition/0.4.8?timeout=700")
.pipe(input)
.then(function(output) {
console.log(output);
});
}
</script>
</body>
</html
How do you want to display in HTML? Only pure JSON? What is the difficulty?
– Rafael Tavares
I get the JSON with the right results, only on the console, so I wanted to pass the result and display it in the HTML page, so that it is visible. Can you help me? I don’t have much knowledge in JSON, thanks in advance.
– Gustavo Daniel
If you don’t know how to create an element in HTML with Javascript, take a look here: https://answall.com/q/120708/100416
– Rafael Tavares
Thanks bro, really. Voou try here, count on me too, I know little but I can help, vlw.
– Gustavo Daniel
Just one more question friend, how do I insert my JSON here in Js? (I know nothing about JS)
– Gustavo Daniel
An example:
const pre = document.createElement('pre');pre.innerText = JSON.stringify(output, null, 4);document.body.append(pre);
. I don’t recommend giving theappend
in thebody
, make it look better in your HTML, this is just an example (are three lines, break them after the;
). If you have difficulties, I suggest you create a simple example (read [mcve]) and click [Edit] to improve your question.– Rafael Tavares
Thanks guy, but the console in the browser says that "Uncaught Referenceerror: output is not defined at (index):38"
– Gustavo Daniel
Gustavodaniel the code that @Rafaeltavares suggested or has to be placed inside the then(’s Function, the method is asynchronous and not synchronous, outside the scope of the variable.
– Guilherme Nascimento