I am new to javascript but I am trying to build a script which can query an API via JSON and AJAX and display the results.
This is the code I have so far: http://jsfiddle.net/spadez/62Ler/7/
$('#search').click(function (event) {
$.ajax({
url: "api.test.com",
type: "GET",
dataType: "json",
timeout: 5000,
context: this,
beforeSend: function () {
$('#content').fadeTo(500, 0.5);
},
success: function (data, textStatus) {
$('html, body').animate({
scrollTop: '0px'
}, 300);
$('#content').html(data.objects[0].category+'<br>'+data.objects[0].company);
},
error: function (x, t, m) {
if (t === "timeout") {
alert("Request timeout");
} else {
alert('Request error');
}
},
complete: function () {
$('#content').fadeTo(500, 1);
}
});
});
Let's say the API has this URL format:
- Base URL: http://api.test.com/v1/search?q=test
- Query for test: http://api.test.com/v1/search?q=test
A typical json response would be as follows:
https://gist.github.com/employ/0b24c1c065d6a671de76
Question
How do I use the contents of my #search input box to send the query to my API? So, if I type "testing" in my input box, how do I make my AJAX script request that query?