tommyboy123x Posted December 17, 2007 Share Posted December 17, 2007 So far it doesn't seem to be working to use the send() to send something to an external server. here is what i have. function ajaxFunction(){ var ajaxRequest; try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } ajaxRequest.open("GET", "http://mysite.com/tester.php?a=1&b=2&c=3", true); ajaxRequest.send(null); } ajaxFunction(); the script this is running on is not mysite.com. Should this be working even though it is on an outside server? Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/ Share on other sites More sharing options...
phpQuestioner Posted December 17, 2007 Share Posted December 17, 2007 I am not 100% sure that this will work; but try it and see. <script language="javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP"); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest(pick) { // Open PHP script for requests http.open('get', 'transfer.php?url='+pick); http.send(null); } window.onload=function() { sendRequest('http://mysite.com/tester.php?a=1&b=2&c=3'); } </script> transfer.php script - PHP 5 Enabled Version: <?php $url = $_GET['url']; $page = file_put_contents("$url"); ?> transfer.php script - PHP 4 or 5 Enabled Versions: <?php $url = $_GET['url']; $page = fopen($url, "w"); fclose($page); ?> or - if that does not work - try this version: <?php $url = $_GET['url']; $page = file_get_contents($url); ?> Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416571 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2007 Share Posted December 17, 2007 Javascript cannot make a http request to a domain that is different from the current page's domain - http://www.mozilla.org/projects/security/components/same-origin.html http://en.wikipedia.org/wiki/Same_origin_policy Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416575 Share on other sites More sharing options...
phpQuestioner Posted December 17, 2007 Share Posted December 17, 2007 Javascript cannot make a http request to a domain that is different from the current page's domain - http://www.mozilla.org/projects/security/components/same-origin.html http://en.wikipedia.org/wiki/Same_origin_policy You can request it - but you have to use a php approach to getting the contents; I believe the script I have above will do just that. Like I say; I cannot guarantee it; but I think so........ Example: <script language="javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP"); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest(pick) { // Open PHP script for requests http.open('get', 'transfer.php?url='+pick); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM the PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("h").innerHTML = response; } } } window.onload=function() { sendRequest('http://www.google.com/search?q=AJAX'); } </script> </head><body> <div id="h" style="width:500px;height:400px;overflow:auto;display:block;border:solid 1px black"></div> transfer.php script example: <?php $url = $_GET['url']; $page = file_get_contents($url); ?> The above code will display Google search results for keyword "AJAX" - this was sent by a query string from my or your domain to Google. Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416577 Share on other sites More sharing options...
dsaba Posted December 17, 2007 Share Posted December 17, 2007 cURL lib in PHP is a nice package and set of functions that can handle many different types of HTTP requests including POST Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416579 Share on other sites More sharing options...
phpQuestioner Posted December 17, 2007 Share Posted December 17, 2007 Javascript cannot make a http request to a domain that is different from the current page's domain - http://www.mozilla.org/projects/security/components/same-origin.html http://en.wikipedia.org/wiki/Same_origin_policy You can request it - but you have to use a php approach to getting the contents; I believe the script I have above will do just that. Like I say; I cannot guarantee it; but I think so........ Example: <script language="javascript"> function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP"); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest(pick) { // Open PHP script for requests http.open('get', 'transfer.php?url='+pick); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM the PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById("h").innerHTML = response; } } } window.onload=function() { sendRequest('http://www.google.com/search?q=AJAX'); } </script> </head><body> <div id="h" style="width:500px;height:400px;overflow:auto;display:block;border:solid 1px black"></div> transfer.php script example: <?php $url = $_GET['url']; $page = file_get_contents($url); echo "$page"; ?> The above code will display Google search results for keyword "AJAX" - this was sent by a query string from my or your domain to Google. PS (Update): Sorry about the double post of the exact same thing - I found a error in my code above and wanted to correct it; so the example would work. I had forgot to echo out the content of my file - my mistake - sorry :-\ Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416582 Share on other sites More sharing options...
Zane Posted December 17, 2007 Share Posted December 17, 2007 the only luck you'll have with transfering/receiving variables to another domain is with cURL Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416590 Share on other sites More sharing options...
dsaba Posted December 17, 2007 Share Posted December 17, 2007 to phpQuestioner: That's a great idea/trick to use PHP to grab the remote contents of a page, and use ajax to request the php page with will in turn request the remote page. the only luck you'll have with transfering/receiving variables to another domain is with cURL you mean with HTTP POST I believe file_get_contents() handles the transfer and retrieval of variables quite well through HTTP GET Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-416619 Share on other sites More sharing options...
tommyboy123x Posted December 17, 2007 Author Share Posted December 17, 2007 Topic solved. I didn't know cURL wouldn't just run, i thought it was almost the same as header(location:...); but with a little more options. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/#findComment-417198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.