开发者

How to access different domain data using Java script

Here is the issue. Suppose there is a DOMAIN A which is going to be the server containing a PHP Script file. The data from Domain A is to be accessed by a Client at DOMAIN B.

I know it cannot be accessed directly using JavaScript. So what I did is, in Domain A I created a a JavaScript file as front-end for the PHP Script which AJAXes the PHP and returns the data. But unfortunately it din't work

I came across an example having PHP as a Middle Man in the client side. But I donot want to keep any server side PHP code as a middle man in the client side. I just want to give out the Javascript to the client domain.

How to get data with JavaScript from another server?

DOMAIN A

PHP - data.php

<?php echo "Server returns data"; ?>

JS - example.js

Does the Ajax to the PHP

function getData()
{
   //assume ajax is done for data.php and data is retrieved, now return the data
   return ajaxed_data;
}

Domain B

JS

Client includes the example.js file from Domain A in his HTML

<script type="text/javascript" src="http://www.DomainA.com/example.js"></script>
<scr开发者_StackOverflowipt type="text/javascript">
     alert(getData());
</script>

I hope I have made myself understandable ! Can this be established ? Its something like Google friend connect, what I mean is, just provide JavaScript to the client and thats it. Every thing carried out in server side

Thankx for providing this forum


You could use JSONP. jQuery has a good support for it.

DOMAIN A - data.php:

<?php
    $data = '{ "data" : "Server returns data" }';
    echo $_GET['jsoncallback'] . '(' . $data . ');';
?>

DOMAIN B - client:

$.getJSON('http://domainA.com/data.php?jsoncallback=?', function(json) {
    alert(json.data);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜