I'm new in this field. I have an API with multiple arrays. I want to use ajax to get all the object of any of the array in html format by just typing the array name in my html input.
{
"players": [
{
"name": "Marcos Alonso",
"position": "Left-Back",
},
{
"name": "Marco van Ginkel",
"position": "Central Midfield",
}
articles": [
{
"author": "Stephen Walter",
"url": "http://www.telegraph.co.uk/news/2017/04/15/disruptive-stag-party-revellers-thrown-plane-manchester-airport/",
},
{
"author": "TMG",
"url": "http://www.telegraph.co.uk/news/2017/04/15/north-korea-marks-anniversary-military-parade-pyongyang-pictures/",
}],
...........
......
}
My index.php looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="clientcss.css" rel="stylesheet" type="text/css"><!-- css styling connection -->
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type=text/javascript >
$(document).ready(function () {
$('#getstuff').click(function () {
var requestdata = $('#choi').val();
var result = $('resultdiv');
$.ajax({
url: 'http://localhost/v1/api/webapi',
method: post,
data: {search: requestdata},
dataType: 'json',
success: function (data) {
result.html('array: ' + data.search);
}
})
});
});
</script>
<title>clientside</title>
</head>
<body>
<div id="clnt">
<h3>Testing testing</h3>
<table>
<input type="text" id="choi" name="chi" placeholder="type something" size="30" required>
</table>
<button type="button" id="getstuff" value="GetSearch">GettheData</button>
<br/><br/>
<div id="resultdiv">
</div>
</div>
</body>
</html>
Kindly help me with the ajax / jQuery to fetch all the objects of a array by typing or parsing the array name.
I want to just type e.g 'players' and get all the objects of the players array in the 'resuldiv' without refreshing the browser.
Thanks for your help.