Implementing Cross site Ajax support for prototype

5 views
Skip to first unread message

Thierry

unread,
Oct 21, 2007, 9:10:42 AM10/21/07
to Prototype: Core
I am looking for cross site ajax support for prototype.

Currently this does not seem to be supported. I think I will rewrite
the code of other libraries to be useful for prototype.

The best functionality out there, which i found so far, are:
http://trainofthoughts.org/blog/2007/04/12/jquery-plugin-xsajax/
http://trainofthoughts.org/repo/export/jquery/jquery.xsajax.js

http://cows-ajax.sourceforge.net/index.php

Suggestions and help would be appreciated off course :)

Tobie Langel

unread,
Oct 21, 2007, 9:59:07 AM10/21/07
to Prototype: Core
Hi,

What would be great would be to include writing capacities similar to
what Google came up with.

I have no idea how complex that is to set up - probably very - nor how
their code is licensed.

Regards,

Tobie

On Oct 21, 3:10 pm, Thierry <thierryschellenb...@gmail.com> wrote:
> I am looking for cross site ajax support for prototype.
>
> Currently this does not seem to be supported. I think I will rewrite
> the code of other libraries to be useful for prototype.
>

> The best functionality out there, which i found so far, are:http://trainofthoughts.org/blog/2007/04/12/jquery-plugin-xsajax/http://trainofthoughts.org/repo/export/jquery/jquery.xsajax.js

Thierry

unread,
Oct 21, 2007, 10:03:43 AM10/21/07
to Prototype: Core
What did google come up with?

Tobie Langel

unread,
Oct 21, 2007, 10:21:54 AM10/21/07
to Prototype: Core

Thierry

unread,
Oct 21, 2007, 11:16:21 AM10/21/07
to Prototype: Core
Interesting,

Im trying to understand the ajax classes now, so i can closely mimic
the implementation.

Thierry

unread,
Oct 21, 2007, 12:00:55 PM10/21/07
to Prototype: Core
Does anyone know if it possible to get the header response code, when
using the script technique?

Cheers,
Thierry

Mislav Marohnić

unread,
Oct 21, 2007, 12:08:58 PM10/21/07
to prototy...@googlegroups.com
On 10/21/07, Thierry <thierrysc...@gmail.com> wrote:

Does anyone know if it possible to get the header response code, when
using the script technique?

No ... nothing is possible using the script technique except getting raw data.

Thierry

unread,
Oct 22, 2007, 4:02:52 AM10/22/07
to Prototype: Core
Looks like i can do the readyStates though, which fortunately is all i
need

Thierry

unread,
Oct 22, 2007, 5:29:00 PM10/22/07
to Prototype: Core
Ok here a first start of the work, most of it is ok, but still need to
translate IE support and Safari:

Change in prototype.js ajax.request initialize to:
initialize: function(url, options) {
this.setOptions(options);
this.transport = (!this.options.crosssite) ? Ajax.getTransport() :
new MyXajaxTransportClass;
this.request(url);
},

Now when specifying your javascript, go like this:

function ajaxTest() {
new Ajax.Request('/frontend_dev.php/comment/giveratingjs', {
method: 'GET',
crosssite: true,
parameters: { theorder: '1', down: '1', threadid: '5' },
onLoading: function() {
},
onSuccess: function(transport) {
},
onFailure: function(transport) {
}
});
}

The magic happens in the MyXajaxTransportClass (ill rename it soon),
have a look at the current version:
Still need to improve some things

MyXajaxTransportClass = Class.create();
//modeled after XmlHttpRequest http://en.wikipedia.org/wiki/XMLHttpRequest
//functions open, send (setRequestHeader) - variable readyState,
status
//
// * 0 = uninitialized - open() has not yet been called.
// * 1 = open - send() has not yet been called.
// * 2 = sent - send() has been called, headers and status are
available.
// * 3 = receiving - Downloading, responseText holds partial data.
// * 4 = loaded - Finished.

//TODO:
//Implementation for indicating Failure
//Delayed removal of <script> nodes

//
//------------------------------ initialize, open and send
------------------------------------------------------
//
MyXajaxTransportClass.prototype.initialize = function() {
this.readyState = 0;
}

MyXajaxTransportClass.prototype.open = function(method, url,
asynchronous) {
if(method != 'GET') alert('Method should be set to GET when using
cross site ajax');
this.readyState = 1;
this.onreadystatechange();
this.url = url;
this.userAgent = navigator.userAgent.toLowerCase();
this.setBrowser();
this.prepareGetScriptXS();
}

MyXajaxTransportClass.prototype.send = function(body) {
this.readyState = 2;
this.onreadystatechange();
this.getScriptXS(this.url);
}

//
//------------------------------ actually do the request: setBrowser,
prepareGetScriptXS, callback, getScriptXS ----------
//

// Figure out what browser is being used, is this good practise?
MyXajaxTransportClass.prototype.setBrowser = function(body) {
MyXajaxTransportClass.prototype.browser = {
version: (this.userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) ||
[])[1],
safari: /webkit/.test(this.userAgent),
opera: /opera/.test(this.userAgent),
msie: /msie/.test(this.userAgent) && !/opera/.test(this.userAgent),
mozilla: /mozilla/.test(this.userAgent) && !/(compatible|
webkit)/.test(this.userAgent),
konqueror: this.userAgent.match(/konqueror/i)
};
}

MyXajaxTransportClass.prototype.prepareGetScriptXS = function () {
if (this.browser.safari || this.browser.konqueror) {
this._xsajax$node = [];
this._xsajax$nodes = 0;
}
}

//node helper for safari needs fixing

MyXajaxTransportClass.prototype.callback = function () {
this.readyState = 4;
this.onreadystatechange();
//TODO, timed cleanup
//$(this).remove();

}

MyXajaxTransportClass.prototype.getScriptXS = function () {
/* determine arguments */
var arg = {
'url': null
};
if (typeof arguments[0] == "string") {
/* simple usage, is all we need */
arg.url = arguments[0];
}

/* generate <script> node */
this.node = document.createElement('SCRIPT');
this.node.type = 'text/javascript';
this.node.src = arg.url;


/* optionally apply event handler to <script> node for
garbage collecting <script> node after loading and/or
calling a custom callback function */
var node_helper = null;


if (this.browser.msie) {
/* MSIE doesn't support the "onload" event on
<script> nodes, but it at least supports an
"onreadystatechange" event instead. But notice:
according to the MSDN documentation we would have
to look for the state "complete", but in practice
for <script> the state transitions from "loading"
to "loaded". So, we check for both here... */
try{this.node.get(this[0]).onreadystatechange = function () {
if (this.readyState == "complete" || this.readyState ==
"loaded")
this.callback();
};} catch(e) {dump(e)}
}
else if (this.browser.safari || this.browser.konqueror) {
/* Safari/WebKit and Konqueror/KHTML do not emit
_any_ events at all, but we can exploit the fact
that dynamically generated <script> DOM nodes
are executed in sequence (although the scripts
theirself are still loaded in parallel) */
this._xsajax$nodes++;
var helper =
'var ctx = jQuery._xsajax$node[' + this._xsajax$nodes +
'];' +
'ctx.callback.call(ctx.node);' +
'setTimeout(function () {' +
' jQuery(ctx.node_helper).remove();' +
'}, 100);';
node_helper =
$(document.createElement('script'))
.attr('type', 'text/javascript')
.text(helper);
this._xsajax$node[this._xsajax$nodes] = {
callback: callback,
node: thi.node.get(0),
node_helper: node_helper.get(0)
};
}
else {
/* Firefox, Opera and other reasonable browsers can
use the regular "onload" event... */
this.node.onload = this.callback.bind(this);
}

/* inject <script> node into <head> of document */
this.readyState = 3;
this.onreadystatechange();
var head = document.getElementsByTagName('HEAD')[0] ;
head.appendChild(this.node);

/* optionally inject helper <script> node into <head>
(Notice: we have to use a strange indirection via
setTimeout() to insert this second <script> node here or
at least Konqueror (and perhaps also Safari) for unknown
reasons will not execute the first <script> node at all) */
if (node_helper !== null) {
setTimeout(function () {
var head = document.getElementsByTagName('HEAD')[0] ;
head.appendChild(node_helper);
}, 100);
}

}

//
//------------------------------ Don't complain when these are called:
setRequestHeader and onreadystatechange ----------
//

MyXajaxTransportClass.prototype.setRequestHeader = function() {
//we don't need this
}
MyXajaxTransportClass.prototype.onreadystatechange = function() {
//we don't need this
}


Thierry

unread,
Oct 25, 2007, 5:00:58 PM10/25/07
to Prototype: Core

Justin

unread,
Nov 8, 2007, 3:26:19 AM11/8/07
to Prototype: Core
That isn't the same thing as the SlipSpace / Google method -
http://www2007.org/papers/paper801.pdf. That uses nested iframes to
ensure that harmful scripts can not be accessed. Kris Zyp has
implemented a version of it Cross Safe - http://ajaxian.com/archives/crosssafe.

On Oct 25, 3:00 pm, Thierry <thierryschellenb...@gmail.com> wrote:
> Done :)
>
> http://www.mellowmorning.com/2007/10/25/introducing-a-cross-site-ajax...

Viktor Kojouharov

unread,
Nov 9, 2007, 7:36:45 AM11/9/07
to Prototype: Core
imho this would've probably been better implemented with form +
iframe. That way, not only you can implement some form of sandbox, but
you can also use post requests.

On Nov 8, 10:26 am, Justin <JustinBMe...@gmail.com> wrote:
> That isn't the same thing as the SlipSpace / Google method -http://www2007.org/papers/paper801.pdf. That uses nested iframes to


> ensure that harmful scripts can not be accessed. Kris Zyp has

> implemented a version of it Cross Safe -http://ajaxian.com/archives/crosssafe.

Reply all
Reply to author
Forward
0 new messages