È possibile utilizzare __utils__.sendAJAX()
:
var casper = require('casper').create();
var wsurl = 'https://raw.github.com/n1k0/casperjs/master/package.json';
var word;
casper.start('http://google.com/', function() {
word = this.evaluate(function(wsurl) {
try {
return JSON.parse(__utils__.sendAJAX(wsurl, 'GET', null, false)).name;
} catch (e) {
}
}, {wsurl: wsurl});
});
casper.then(function() {
if (!word) {
this.die('unable to retrieve word');
}
this.echo('searching for ' + word);
this.fill('form[action="/search"]', {q: word}, true);
});
casper.run(function() {
this.echo(this.getCurrentUrl());
this.exit();
});
esecuzione del campione (non dimenticate di passare --web-security=no
):
$ casperjs test.js --web-security=no
searching for casperjs
http://www.google.fr/search?hl=fr&source=hp&q=casperjs&gbv=2&oq=&gs_l=
Speranza che aiuta.
utilizzando xmlhttprequest? – NiKo
Ecco come lo farei se usassi jQuery. Non so il modo ottimale per farlo con CasperJS. Tieni presente che si tratta di una richiesta interdominio, che non mi aspetto essere un problema all'interno di Casper. – eComEvo