Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/81974-ajax-send-to-external-server/
Share on other sites

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);

?>

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

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.

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 :-\

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.