I got this code online and it seems to do what i'm trying to achieve. However, the function calls onload. I want it to work only when i click a button. Something as simple as:
<button type="submit" form="form" value="Submit">Submit</button>
This is the JS:
$.ajax("https://api.base62.io/encode", {
type: "POST",
data: JSON.stringify({ data: "Hello world!" }),
contentType: "application/json",
}).done(function (data) {
$("#encoded").html(data.encoded);
}).fail(function (xhr, status, error) {
$("#error").html("Could not reach the API: " + error);
});
HTML:
<p id="encoded">Encoding..</p>
<p id="error"></p>