Page 1 of 1

jQuery my server from another server

Posted: Thu Feb 17, 2011 12:53 pm
by psychotomus
I am trying to return the results of a php file after sending a jquery request to it.

The page: http://simstests.zxq.net/

My js file when button is clicked located on my server:

Code: Select all

$(document).ready(function(){			
	$("#attack").mouseup(function () {
		$("#outcome").html("Attacking...");
		$.ajax({
		      url: "http://www.phpengines.info/vPet2/ajax/petaction.php",
		      global: false,
		      type: "POST",
		      dataType: "html",
		      success: function(msg){     		         
		      	 $("#outcome").html(msg);		         
		      },
		      error: function(msg) {
		      	$("#outcome").html(msg);
		      }
   		});
	});
	$("#heal").mouseup(function () {
		$("#outcome").html("Healing...");
		$.ajax({
		      url: "http://www.phpengines.info/vPet2/ajax/petaction.php",
		      global: false,
		      type: "POST",
		      dataType: "html",
		      success: function(msg){     		         
		      	 $("#outcome").html(msg);		         
		      },
		      error: function(msg) {
		      	$("#outcome").html(msg);
		      }
   		});
	});

});	

my file petaction.php

Code: Select all

die("test");
now when clicking the button it says Healing or Attacking, but it doesn't say "test" afterwards...

Re: jQuery my server from another server

Posted: Fri Feb 18, 2011 3:48 pm
by Jonah Bron
It appears your script is on simstests.zxq.net, while you're calling a page phpengines.info. You can't do that. If you need to access a page on another domain, call a PHP script on the same server, and call the remote file with cURL/file_get_contents() from there.

Re: jQuery my server from another server

Posted: Fri Feb 18, 2011 5:37 pm
by Weirdan
Jonah Bron wrote:It appears your script is on simstests.zxq.net, while you're calling a page phpengines.info. You can't do that.
You can with json-p

Re: jQuery my server from another server

Posted: Tue Feb 22, 2011 12:23 pm
by psychotomus
Weirdan wrote:
Jonah Bron wrote:It appears your script is on simstests.zxq.net, while you're calling a page phpengines.info. You can't do that.
You can with json-p
Do you have any sort of example on how to do this? ;]

Re: jQuery my server from another server

Posted: Tue Feb 22, 2011 3:03 pm
by Weirdan
psychotomus wrote:Do you have any sort of example on how to do this? ;]
The idea is quite simple: while you cannot make crossdomain requests with xmlhttprequest, you can point src attribute of a script tag to another domain and get browser to fetch data and attempt to execute it as if it was javascript code. You still cannot read the response, but since script is executed it may call some function you define upfront with the required data.

Simple example:
html page (hosted at example.com):

Code: Select all

<html>
<head>
<script type="text/javascript">
function doSomething(data) {
     console.log(data);
}
</script>
<script type="text/javascript" src="http://thirdparty.com/script.php?callback=doSomething"></script>
</head>
<body>
</body>
</html>
script.php (hosted at thirdparty.com):

Code: Select all

<?php
$data = array(
    'message' => 'Hello from thirdparty.com',
    'code' => 123,
);
// callback sanitization is omitted here for brevity
// in a production quality code you MUST sanitize the input
echo $_GET['callback'] . '(' . json_encode($data) . ');';