JPProxy - tiny jsonp proxy

JPProxy is a very simple yet powerful JSONP script. It allows you to make ajax like requests to any page on a server that has the jpproxy.php script on it. I tried really hard to make it as simple and generic as possible so the source is tiny.

1. Client

A script tag is injected into the DOM and all the values are added to the url as GET parameters.

<script type='text/javascript' src='http://server/jpproxy.php?callback_id=1&page=somepage.php&data={"key":"value"}'>

2. Server

The $_GET["data"] key/value pairs are assigned to $_GET and $_POST variables. Then the requested page is included as a script so it can use the GET and POST paramters the way it normaly would. Finally javascript is generated which passes the callback_id and result data to the callback function

  echo "JPProxy.callback(" . $_GET["callback_id"] .",$result);";

3. Client

The generated javascript get’s executed and your callback function is called with the response data.


we have a page on and we want the page to grab

Drop jpproxy.php in the root directory ( )

Example Client Code:

//callback to handle the data once it arrives
function mycallback(data){

//POST and GET parameter passed to data.php
var parameters = {
    foo: "bar",
    bar: "poop",

//set the proxy location

//note: the page's path must be relative to jpproxy.php"path/data.php", parameters, mycallback);



var JPProxy = {
    request_id: 0,
    callbacks: [],
    scripts: [],
    proxy: null,
    setProxy: function(proxy){
        this.proxy = proxy;
    callback: function(id,data){
        delete this.callbacks[id];
        delete this.scripts[id];
    get: function(page,data,callback){
        this.callbacks[++this.request_id] = callback;
        var script = document.createElement('script');
        var src = this.proxy + '?page=' + encodeURIComponent(page) + '&callback_id=';
        src += this.request_id.toString() + '&data=' + encodeURIComponent(JSON.stringify(data));
        script.setAttribute('type', 'text/javascript');
        this.scripts[this.request_id] = script;


$data = json_decode($_GET["data"]);
$obj = "null";
if ($data && is_object($data) && is_file($_GET["page"])) {
    foreach ($data as $key => $value) { $_GET[$key] => $_POST[$key] = $value; }
    require $_GET["page"];
    $obj = json_encode(ob_get_clean());
echo "JPProxy.callback(" . $_GET["callback_id"] . ", $obj);";