Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  32] [ 6]  / answers: 1 / hits: 113742  / 12 Years ago, thu, july 12, 2012, 12:00:00

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?


More From » json

 Answers
37

An example using jQuery is below. Hope this helps



<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html>
<title>My jQuery JSON Web Page</title>
<head>
<script type=text/javascript src=https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script>
<script type=text/javascript>

JSONTest = function() {

var resultDiv = $(#resultDivContainer);

$.ajax({
url: https://example.com/api/,
type: POST,
data: { apiKey: 23462, method: example, ip: 208.74.35.5 },
dataType: json,
success: function (result) {
switch (result) {
case true:
processResponse(result);
break;
default:
resultDiv.html(result);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
};

</script>
</head>
<body>

<h1>My jQuery JSON Web Page</h1>

<div id=resultDivContainer></div>

<button type=button onclick=JSONTest()>JSON</button>

</body>
</html>


Firebug debug process



Firebug


[#84299] Wednesday, July 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsleyashlynnh

Total Points: 64
Total Questions: 119
Total Answers: 98

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;