-
Website
http://www.phpfour.com/blog -
Original page
http://www.phpfour.com/blog/2008/03/cross-domain-ajax-using-php/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
robskils
1 comment · 1 points
-
joshsharp
1 comment · 1 points
-
nirjhar
1 comment · 1 points
-
Nhm Tanveer Hossain Khan
1 comment · 1 points
-
cubanhenry5
1 comment · 1 points
-
-
Popular Threads
Good Work Batman!
If you want to get json data , then you can easily use getJSON.
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",function(data){
$.each(data.items, function(i,item){
$("").attr("src", item.media.m).appendTo("#images");
});
});
Or if you don’t want to get other type of data like XML or other type, then you have to use cURL in PHP:
$.ajax({type: "GET",
url : ”http://myserver.com/transport.php?action=’ +
urlencode(‘different-server.com/return_call.php’) +
‘&method=get&data1=101&data2=pass”,
success: function(serverResponse)
/*parse serverResponse data */
});
No comment ?
But I would suggest using $_SERVER['REMOTE_ADDR'] to block request from scripts other than the server I'm invoking it from.
The reason I think that way is, in the project I used it, the other server was also mine and I needed to communicate between them. As the output was quite dynamic, caching couldn't be done.
But then again, if needed, caching can be added easily. I will be uploading one with caching later tonight.
Thanks everybody for commenting !
What's important to note, too, is that if curl_exec can't fetch the data for some reasons it will return false. That means that your script will echo nothing. People using it should take care on the javascript side to check the data they get and if it's empty display an error.
You must change that line :
$action = $_REQUEST['url'];
To :
$action = $_REQUEST['action'];
Or change this :
xmlHttp.open(“GET”, ‘http://myserver.com/transport.php?action=’ + 10: urlencode(‘different-server.com/return_call.php’) + 11: ‘&method=get&data1=101&data2=pass’, true );
With this :
xmlHttp.open(“GET”, ‘http://myserver.com/transport.php?url=’ + 10: urlencode(‘different-server.com/return_call.php’) + 11: ‘&method=get&data1=101&data2=pass’, true );
you wrote this:
if ($key != 'url' || $key != 'method')
while you obviously meant this:
if ($key != 'url' && $key != 'method')
(since ($key != 'url' || $key != 'method') is always true)
Thanks.
Is there anything you know of that causes this freezing/hanging?
Thank you
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","transport.php?action="+urlencode("http://www.netshelter.net/time.php"),true);
xmlHttp.send(null);
}
</script>
<form name="myForm">
Name: <input type="text" onkeyup="ajaxFunction();" name="username" />
Time: <input type="text" name="time" />
</form>
</body>
</html>