<aside> 💡 다른 도메인으로부터 데이터를 가져오기 위해 사용하는 방법
</aside>
<?php
$data='[
{"id":"1","name":"jjanggu","email":"[email protected]"},
{"id":"2","name":"maenggu","email":"[email protected]"},
{"id":"3","name":"younggu","email":"[email protected]"},
]';
//.은 문자열 연결
//$_GET["callback"] 은 HTML 에서 get 방식으로 전송한 데이터를 받을 메개변수
echo $_GET["callback"]."(".$data.")";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
아래의 내용
</script>
</head>
<body>
</body>
</html>
$(function(){
var href="http://green04.dothome.co.kr/member.php?callback=myFnc";
$.ajax({
url:href,
dataType:'jsonp'
}).done(function myFnc(data){
console.log(data);
})
})

실행화면

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function(){
var href="http://green04.dothome.co.kr/member.php?callback=myFnc";
$.ajax({
url:href,
dataType:'jsonp'
}).done(function myFnc(data){
console.log(data);
})
})
</script>
</head>
<body>
<!-- 추가 -->
<div class="wrap"></div>
</body>
</html>
<script>
$(function(){
var href="http://green04.dothome.co.kr/member.php?callback=myFnc";
$.ajax({
url:href,
dataType:'jsonp'
}).done(function myFnc(data){
if(data.length>0){
var tb=$('<table />')
data.forEach(function(o){
$id=o.id;
$name=o.name;
$email=o.email;
var tr=$('<tr/>');
tr.append(
$('<td/>').text($id),
$('<td/>').text($name),
$('<td/>').text($email)
);
tb.append(tr)
})
$('.wrap').append(tb);
}
})
})
</script>
실행화면

XSS 크로스 도메인 ajax 연동 방법 - jsonp 예제자르르 2019. 1. 30. 11:36