最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Need to send data from one website to another asynchronously - Stack Overflow

programmeradmin0浏览0评论

My script tracks clicks on a client's page and uploads this to my server(let's call it .)

Now I want the client to put a script on his site and this script will tag the clicks on the page and send the data to . on page leave or maybe even from time to time.

I can't use Ajax because of cross browser restrictions, ofcourse. So how do I do this? I'll give a little hypothetical code to help you understand. I have a closure tracker which is loaded when the user puts

<script src=".php?client_id=2332"></script>

The code loads the closure tracker=function(){....}; The user needs to create an object of tracker

<script>
track=new tracker();
track.init();
</script>

track.init() basically binds a function that gets the Xpath of the element and stores it in track.clicks . Now i need to have a function track.send() which will send the clicks array as json(I know how to convert it to json) and send it on page close.

How do I do this. Also I have seen some sites use something like:

 document.write("<sc"+"ript"+" src='.php?client_id=2332'"+"></scr"+"ipt>";

Why do this instead of a direct script tag? And why not put it in one string if we must use document.write() at all.

So the two questions are:

  1. How do I send the data asyncronously(or otherwise) on page load?
  2. What does the above code do?
  3. I've seen Google analytics and Facebook do this, they don't seem to have any problems. Facebook infact renders the dialog box on screen. How?

My script tracks clicks on a client's page and uploads this to my server(let's call it .http://fancyclickserver23)

Now I want the client to put a script on his site and this script will tag the clicks on the page and send the data to .http://fancyclickserver23 on page leave or maybe even from time to time.

I can't use Ajax because of cross browser restrictions, ofcourse. So how do I do this? I'll give a little hypothetical code to help you understand. I have a closure tracker which is loaded when the user puts

<script src="http://fancyclickserver23/loadtracker.php?client_id=2332"></script>

The code loads the closure tracker=function(){....}; The user needs to create an object of tracker

<script>
track=new tracker();
track.init();
</script>

track.init() basically binds a function that gets the Xpath of the element and stores it in track.clicks . Now i need to have a function track.send() which will send the clicks array as json(I know how to convert it to json) and send it on page close.

How do I do this. Also I have seen some sites use something like:

 document.write("<sc"+"ript"+" src='https://fancyclickserver23/loadtracker.php?client_id=2332'"+"></scr"+"ipt>";

Why do this instead of a direct script tag? And why not put it in one string if we must use document.write() at all.

So the two questions are:

  1. How do I send the data asyncronously(or otherwise) on page load?
  2. What does the above code do?
  3. I've seen Google analytics and Facebook do this, they don't seem to have any problems. Facebook infact renders the dialog box on screen. How?
Share Improve this question asked Feb 9, 2012 at 15:00 SoWhatSoWhat 5,6222 gold badges30 silver badges62 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3 +50

Despite the cross-domain security restrictions, you can still use AJAX (XHR) to some extent.

Browser can not trash a cross-domain request just like that. It has to see what the server thinks of it first. It does this by sending the actual request over to the server and receiving the instructions along with the response in HTTP headers. If the Access-Control-Allow-Origin header doesn't arrive or doesn't match the origin domain, browser trashes the response and throws out a security warning in the console.

From my personal experience, Access-Control-Allow-Origin header or not, when site aaa. sends an AJAX request to site bbb. the one-way delivery of the data payload to the server happens anyway. It's just a monologue, but what more do one need for collection of data, right? ;)

I, for example, utilize the one-way technique for something similar to yours - I collect JSON reports of unit tests results of my open-source project here. You can spot the security warning in the console, but at that time the report is already stored safely on the server.

use :

function sendData( data ){
  var scr = document.getElementById("scriptExchange");
  if(typeof scr == "undefined"){
     var scr=document.createEelement("script");
     scr.id="scriptExchange";
     document.body.appendChild(scr);
     scr = document.getElementById("scriptExchange");
  }
  scr.src = "http://another-website./?query="+data;

}

whenever you wanna send data call this funciton . .

now on the other server set the response to a pure JS code e.g.

alert("Data Received");

and in from the other server put the

If you can turn your array of click data into a query string, you could do the following:

var img = new Image(1,1);
img.src = "http://example./clicks-receiver?" + query_string;

You can use jQuery and jsonP to achieve this.

1) make Ajax call like following:

var url = your_other_domain_url + "jsoncallback=?";
var params = {param1:val1, param2:val2};
$.ajax({
  type: 'POST',
  url: url,
  data: params,
  success: function(data){
            //success jsonp handler - assume content in data.response
            if(data.response){
               //do something with the remote site content
            }
  },
  dataType: "json"
});

2) at server side, track any request that has parameter 'jsoncallback' and put the response inside the json return object such as

{status: 'success', response: "<html>.....resposne conent...</html>"}

Isn't it possible to use an iframe. The parent has visibility into the iframe, the iframe can run scripts back to the other server without jumping through a bunch of hoops. I believe our omniture implementation uses this method.

The img method as mentioned above is also a potential.

An older article on it:

http://www.codecouch./2008/10/cross-site-scripting-xss-using-iframes/

or another

http://blog.kotowicz/2010/11/xss-track-how-to-quietly-track-whole.html

Is it bad form to spread this stuff?

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>