I'm attempting to use JSON to initiate a POST request to an API.
I've found some example code, and before I get too far I wanted to get that working, but I'm stuck...
<html>
<head>
<script type="text/javascript">
function JSONTest()
{
requestNumber = JSONRequest.post(
"https://example.com/api/",
{
apikey: "23462",
method: "example",
ip: "208.74.35.5"
},
function (requestNumber, value, exception) {
if (value) {
processResponse(value);
} else {
processError(exception);
}
}
);
}
</script>
</head>
<body>
<h1>My JSON Web Page</h1>
<button type="button" onclick="JSONTest()">JSON</button>
</body>
</html>
This is a .html file, which I am running in chrome. Nothing happens when I click the button...
I think I'm missing a piece of javascript which interprets the JSON response and can be displayed? otherwise any other advice?
This question is related to
javascript
json
api
post
Modern browsers do not currently implement JSONRequest (as far as I know) since it is only a draft right now. I have found someone who has implemented it as a library that you can include in your page: http://devpro.it/JSON/files/JSONRequest-js.html (please note that it has a few dependencies).
Otherwise, you might want to go with another JS library like jQuery or Mootools.
Source: Stackoverflow.com