Jsonp 크로스 도메인 jQuery, Ajax 예제
페이지 정보
작성자 오원장
쪽지보내기
메일보내기
자기소개
아이디로 검색
전체게시물
        댓글  0건
        조회 5,376회
        작성일 12-02-29 18:18
 오원장
쪽지보내기
메일보내기
자기소개
아이디로 검색
전체게시물
        댓글  0건
        조회 5,376회
        작성일 12-02-29 18:18
    본문
jsonp.php
====================jsonp.php 소스===================================
<html>
<head>
<title>JSONP 테스트</title>
<meta http-equiv="content-type" content="text/html; charset=euc-kr">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script language="javascript">
$(function(){
    $("#btn1").click(function(){
 $.getJSON('http://도메인/getJsonp.php', 'callback=?', function(data){
     $.show(data);
 });
    });
 
    $("#btn2").click(function(){
        $.ajax({
            url : 'http://도메인/getJsonp.php'
            , dataType : 'jsonp'
            , jsonp : 'callback'
            , success : function(data){
                $.show(data);
            }
            , error: function(xhr){
                $.show(xhr);
            }
        });
    });
    
    $.show = function(data){
        $.each(data, function(key, val){
            $("#jsonp").append("<div>"+key + " : " + val +"</div>");
        });
    }
 
});
</script>
</head>
<body>
    <button id="btn1">테스트getJSON</button>
    <button id="btn2">테스트ajax</button>
    <div id="jsonp"></div>
</body>
</html>
========== getJsonp.php 소스 ============================
<?php
header("Content-Type:text/html;charset=EUC-KR");
?>
<?
$callback = $_GET["callback"];
echo "$callback ({'key1':'어머니','key2':'아버지'})";
?>
댓글목록
등록된 댓글이 없습니다.

