The HTTP request to get synonyms can be implemented in Javascript using different solutions:
Plain javascript request
jQuery based request
Plain Javascript
The Javascript example requires support for JSON in browser, e.g. Firefox 3.5 can handle JSON.
The example is quite simple, it invokes the thesaurus API with callback=process:
var s = document.createElement("script");
s.src = "http://thesaurus.altervista.org/thesaurus/v1?word=peace&language=en_US&output=json&key=test_only&callback=process"; // NOTE: replace test_only with your own KEY document.getElementsByTagName("head")[0].appendChild(s);
function process(result) {
output = "";
for (key in result.response) {
list = result.response[key].list;
output += list.synonyms+"<br>";
}
if (output)
document.getElementById("synonyms").innerHTML = output;
}
Here is the live demo, that you will be able to see only on JSON capable browsers:
Synonyms of peace:
please wait...
jQuery based request
jQuery is a powerful javascript library designed to simplify the client-side scripting of HTML.
The following code snippet shows how to perform ajax request to get synonyms:
$.ajax({
url: "https://thesaurus.altervista.org/thesaurus/v1?word=peace&language=en_US&output=json&key=test_only", // NOTE: replace test_only with your own KEY success: function(data){
if (data.length != 0) {
output = "";
for (key in data.response) {
output += data.response[key].list.synonyms+"<br>";
}
$("#div1").html(output);
} else $("#div1").html("empty data");
},
error: function(xhr, status, error){
$("#div1").html("An error occured: " + status + " " + error);
}
});