Skip to content

Instantly share code, notes, and snippets.

@aurelijusb
Created December 2, 2015 19:43
Show Gist options
  • Save aurelijusb/443892f81221ff3b61fc to your computer and use it in GitHub Desktop.
Save aurelijusb/443892f81221ff3b61fc to your computer and use it in GitHub Desktop.
Server-Client (server-localhost) example
<?php
header('Access-Control-Allow-Origin: *');
echo $_GET['a'] + $_GET['b'];

Example for prezentation: Inverting Client-Server Architecture

Installing

Copy server-client.html into your server. E.g. it is http://aurelijus.banelis.lt/server-client.html Run php -S localhost:12345 localhost-server.php You should see 3 in http://localhost:12345/?a=1&b=2 You should see 1+2 = 3 when opened server-client.html on your own server.

Main point

Access-Control-Allow-Origin: * allows cross site scripting.

More info

Prezentation http://aurelijus.banelis.lt/prezentations/client-server-2015/inverting-client-server.pdf

<html>
<head>
<title>Server-Client demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
var host = 'http://localhost:12345';
var a = 1;
var b = 2;
$(document).ready(function(){
$.ajax({
url: host + '/?a=' + a + '&b=' + b,
async: false,
success: function(result){
$('div').html(a + '+' + b + ' = ' + result);
},
error: function(xhr, message, err) {
$('div').html("Error: " + xhr.statusText);
}
});
});
</script>
</head>
<body>
<div><h2>Loading...</h2></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment