views:

226

answers:

5

I am creating a analytic project. My goal is to give the owner of x-domain a small amount of javascript includes from my site. That gives me the ability trace their mouse movements. I got the tracing down, All I need to do is send the data back to my server so it can be stored in my DB. my problem is the data is too large to send through getJSON.

Remember.. I can't use $.Post, or any kind of XMLhttp request because my domain and x-domain are REMOTE. And Browser don't permit that.. I can only use getJSON.

Since that doesn't work, I was told to setup a proxy. Well from what I've learned, a proxy only works for the server that has the proxy setup. Not for the server that is trying to send me the data

I know this is possible, cause ive seen it. anyone have any ideas ?? Is iframes good for what I am trying to do ?? Does anyone have any resources to share ??

Thanks alot

A: 

Have you considered using something like JSONP?

Jeremy Morgan
A: 

Split your data in chunks so that getJSON would work. You could implement data queuing, so that the producer keeps filling a queue with data, and the consumer on your domain gets it in smaller chunks with getJSON. It won't be real - time, but you could try it and see if you're ok with the performance.

luvieere
That seems reasonable. But and I did think about that. But if I do it that way. I would have to let my server know that each request coming in is from the same session. If I do not do it this, it may insert a new row in the DB for each data coming through. I would have to send a cookie through that identifies that it's coming from the same session. What that seem reasonable.
numerical25
Or generate a (semi-)unique id at the client and send the same id with all parts. The server can then concatenate parts with the same id.
Wim
A: 

You can use javascript to talk to flash and have flash do the cross-domain part see http://www.xml.com/pub/a/2006/06/28/flashxmlhttprequest-proxy-to-the-rescue.html

I'm not clear on why you can't use a proxy. The javascript in the browser posts to a script running on x-domain and that script then posts the exact same info to your domain using curl or similar.

dsas
I am not sure either. Suppose the php file was not made for income Ajax calls but for in outgoing. I tried it on sites like Google from the my server using it and it worked. But I tried using it from a x-domain coming in and it didn't.
numerical25
So the problem wasn't with the concept of a proxy but with a particular implementation of one? Maybe we can help you fix that implementation.
dsas
A: 

You can have your JavaScript create an iframe and a form, then have the form post into the iframe. You can position it off screen to make is hidden. For instance:

function post_my_data(json){

    var f = document.createElement('form');
    f.action = 'http://www.my-domain.com/receive.php';
    f.method = 'post';
    f.target = 'posttarget';

    var i = document.createElement('input');
    i.type = 'hidden';
    i.name = 'json';
    i.value = json;

    f.appendChild(i);

    var ifr = document.createElement('iframe');
    ifr.name = 'posttarget';
    ifr.style.display = 'absolute';
    ifr.style.left = '-1000px';

    document.body.appendChild(ifr);
    document.body.appendChild(f);

    f.submit();
}
Cal
I heard about iframes, but I didn't know where to start. I think this will help me a lot. Not sure if it will work. But it certainly seems like a good start. thanx alot
numerical25
if you know of any other great examples, perhaps a book or URL you could refer me to, please do and thanx
numerical25
there are plenty of hits if you google "using a hidden form to post data cross domain", e.g. http://w-shadow.com/blog/2008/11/20/cross-domain-post-with-javascript/ but the technique is pretty simple. the downside is that you can't get back any data from the post, so it's a one-way flow only.
Cal
I tried it out, but I Think there might be something wrong with the lineifr.style.display = 'absolute';cause i get the error Error in parsing value for 'display'. Declaration dropped.Not sure if that would cause it not to send. but It didnt send it. I am also using jquery. So not sure if that could cause it. I encapuslated the data in the following. $('#testReq').click(function(){ })
numerical25
drop me an email with your source code (you can find my address via my profile) and i'll take a look
Cal
A: 

Perhaps you can better understand it then I can. I am good with php, but not to great with sending out different headers and stuff. How it works is you post a ajax post to the proxy file. the proxy file is located on a outside server

      $.ajax({
    type: "POST",
    url: "http://mywebsite.com/ajax-proxy.php",
    data: 'csurl=www.google.com',
    error: function(e) {console.log(e);}, 
    success: function(msg){console.log(msg)}

 });

You also got to pass the csurl which is the url that the proxy forwards you to. in this example I used google. but what I would normal use as the csurl is the directory to where I will store the ajax data

In the proxy file, there is a

$valid_requests = array()

In that array, you state all the urls you want the proxy to approve of. in this example you put www.google.com (Note: has to be exactly the same as csurl parameter or it wont work)

Below is the proxy file

  <?php
/**
 * AJAX Cross Domain (PHP) Proxy 0.6
 *    by Iacovos Constantinou (http://www.iacons.net)
 * 
 * Released under CC-GNU GPL
 */

/**
 * Enables or disables filtering for cross domain requests.
 * Recommended value: true, for security reasons
 */
define('CSAJAX_FILTERS', true);


/**
 * A set of valid cross domain requests
 */
$valid_requests = array(
    'www.google.com'
);

/*** STOP EDITING HERE UNLESS YOU KNOW WHAT YOU ARE DOING ***/

// identify request headers
$request_headers = array();
foreach ( $_SERVER as $key=>$value ) {
    if( substr($key, 0, 5) == 'HTTP_' ) {
     $headername = str_replace('_', ' ', substr($key, 5));
     $headername = str_replace(' ', '-', ucwords(strtolower($headername)));
     $request_headers[$headername] = $value;
    }
}

// identify request method, url and params
$request_method = $_SERVER['REQUEST_METHOD'];
$request_params = ( $request_method == 'GET' ) ? $_GET : $_POST;
$request_url    = urldecode($request_params['csurl']);
$p_request_url  = parse_url($request_url);
unset($request_params['csurl']);

// ignore requests for proxy :)
if ( preg_match('!'. $_SERVER['SCRIPT_NAME'] .'!', $request_url) || empty($request_url) ) {
    exit;
}

// check against valid requests
if ( CSAJAX_FILTERS ) {
    $parsed  = $p_request_url;
    $check_url  = isset($parsed['scheme']) ? $parsed['scheme'] .'://' : '';
    $check_url .= isset($parsed['user']) ? $parsed['user'] . ($parsed['pass'] ? ':'. $parsed['pass']:'') .'@' : '';
    $check_url .= isset($parsed['host']) ? $parsed['host'] : '';
    $check_url .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
    $check_url .= isset($parsed['path']) ? $parsed['path'] : '';
    if ( !in_array($check_url, $valid_requests) ) {
     exit;
    }
}

// append query string for GET requests
if ( $request_method == 'GET' && count($request_params) > 0 && ( !array_key_exists('query', $p_request_url) || empty($p_request_url['query']) ) ) {
    $request_url .= '?'. http_build_query($request_params);
}

// let the request begin
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);   // (re-)send headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // return response
curl_setopt($ch, CURLOPT_HEADER, true);       // enabled response headers

// add post data for POST requests
if ( $request_method == 'POST' ) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_params));
}

// retrieve response (headers and content)
$response = curl_exec($ch);
curl_close($ch);

// split response to header and content
list($response_headers, $response_content) = preg_split('/(\r\n){2}/', $response, 2);

// (re-)send the headers
$response_headers = preg_split('/(\r\n){1}/', $response_headers);
foreach ( $response_headers as $key => $response_header )
    if ( !preg_match('/^(Transfer-Encoding):/', $response_header) ) 
     header($response_header);

// finally, output the content
print($response_content);
?>

Again, if I put http://mywebsite.com/ajax-proxy.php?csurl=www.google.com from within my website. it works fine. or even put it in the url. works fine. but if you call it from an outsite server using ajax post. doesnt work.

numerical25