나는이 분야에서 처음이다. 여러 배열이있는 API가 있습니다. 내 html 입력에 배열 이름을 입력하여 html 형식으로 배열의 모든 객체를 가져 오기 위해 ajax를 사용하고 싶습니다.
{
"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/",
}],
...........
......
}
내 index.php는 다음과 같습니다.
<!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>
배열 이름을 입력하거나 구문 분석하여 배열의 모든 객체를 가져 오도록 ajax / jQuery를 도와주세요.
예를 들어 'players'를 입력하고 브라우저를 새로 고치지 않고 'resuldiv'에 플레이어 배열의 모든 개체를 가져오고 싶습니다.
당신의 도움을 주셔서 감사합니다.
API가 전체 JSON 객체를 반환하는 경우 requestdata
서버 로 전달 해서는 안되며이를 사용하여 응답에서 속성을 추출해야합니다.
$.ajax({
url: 'http://localhost/v1/api/webapi',
method: 'post',
dataType: 'json',
success: function (data) {
var html = '<ul>';
var array = data[requestdata];
if (array) {
$.each(array, function(key, value) {
html += '<li>' + key + ': ' + value + '</li>';
});
result.html(html);
}
}
});
변수를 사용하여 개체 속성에 동적으로 액세스를 참조하세요.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다