0

Possible Duplicate:
Ways to circumvent the same-origin policy

For example i have a 2 domain "http://domain1.com/info.php" and the other one is "http://domain2.com/script.html"

now the first domain "http://domain1.com/info.php" is releasing json encoded data like this

{
    "str_info": [
        {
            "str_name": "Mark",
            "str_age": "22"
        },
        {
            "str_name": "Aliza",
            "str_age": "52"
        }
    ]
}

what the second domain "http://domain2.com/script.html" does is to retrieve and manipulate the data given by the first domain.

i don't know how to connect and retrieve the data came from the first domain which is "http://domain1.com/info.php" to the second domain which is the script. so can you please show me how using jsonp, jquery, ajax if possible

Community
  • 1
  • 1

2 Answers2

0

send the data with a callback function such as

callback({
  "str_info":[{"str_name":"Mark","str_age":"22"},{"str_name":"Aliza","str_age":"52"}]
});

and then on the domain receiving data use the function

<script>
    function callback(i){
    //maniputlate i (json data) here
    }
</script>
<script type="text/javascript" src="http://domain1.com/info.php"/>
Yusaf Khaliq
  • 3,333
  • 11
  • 42
  • 82
0

From jQuery.org

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

Script and JSONP requests are not subject to the same origin policy restrictions.

If working crossdomain you can use a method called JSONP The ajax request adds a parameter ?callback=callbackname and your server answers with

callbackname
(
    {
        "str_info": [
            {
                "str_name": "Mark",
                "str_age": "22"
            },
            {
                "str_name": "Aliza",
                "str_age": "52"
            }
        ]
    }
);

The function then is executed on your client. You can use jQuery to get this done very simple

$.getJSON("http://domain1.com/info.php?callback=?", function(data) {
  alert("success");
});

in PHP you would wrap your result like this (simple example)

echo $_GET['callback'], '(' , json_encode( $data ), ')';
Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77