jQuery my server from another server

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

jQuery my server from another server

Post 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...
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: jQuery my server from another server

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: jQuery my server from another server

Post 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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: jQuery my server from another server

Post 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? ;]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: jQuery my server from another server

Post 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) . ');';
Post Reply