Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It actually <em>is</em> possible to do XHR with PhantomJS when loading from the file system!</p> <p>Straight from <a href="http://code.google.com/p/phantomjs/wiki/Interface">the PhantomJs wiki</a>:</p> <pre><code>--web-security=[yes|no] disables web security and allows cross-domain XHR (default is yes) </code></pre> <p>Also see <a href="http://code.google.com/p/phantomjs/issues/detail?id=291">this issue report for PhantomJs</a> which might shed some light on this topic.</p> <p>Sencha's 'sencha create jsb' command uses PhantomJs (and Ext.Loader uses XHR) and it supports loading from the file system.</p> <pre><code>name: 'app-entry', alias: 'a', description: 'The file or URL path to your application\'s HTML entry point', </code></pre> <p>Checkout <code>[senchasdktools]/compat/command/src/modules/GenerateJSB.js</code> and <code>[senchasdktools]/compat/command/scripts/phantomjs-jsb.js</code>.</p> <p>I don't see anything related to the mentioned <code>web-security</code> switch though. Maybe they use a custom phantomJs build.</p> <p><strong>UPDATE</strong> This code fragment allows XHR requests to the file system. Tested with the latest version of phantomJs (1.6.1/Windows):</p> <pre><code>var page = new WebPage(); page.settings.localToRemoteUrlAccessEnabled = true; </code></pre> <p><strong>UPDATE2</strong> This is a working example.</p> <p>Put everything in the same folder, add a <code>test.txt</code> file with some content, then run </p> <pre><code>phantomjs script.js test.html </code></pre> <p>phantomjs script (script.js):</p> <pre><code>var fs = require('fs'); var appLocation = phantom.args[0]; var page = new WebPage(); page.settings.localToRemoteUrlAccessEnabled = true; page.settings.ignoreSslErrors = true; page.onConsoleMessage = function(message, url, lineNumber) { console.log((url ? url + " " : "") + (lineNumber ? lineNumber + ": " : "") + message); }; page.onError = function (msg, trace) { console.log(msg); trace.forEach(function(item) { console.log(' ', item.file, ':', item.line); }); phantom.exit(1); }; if (!/^file:\/\/|http(s?):\/\//.test(appLocation)) { appLocation = 'file:///' + fs.absolute(appLocation).replace(/\\/g, '/'); } page.open(appLocation, function(status) { if (status !== 'success') { error("Failed opening: '" + appLocation + "'. Please verify that the URI is valid"); } page.evaluate(function() { window.onerror = function(message, url, lineNumber) { console.log((url ? url + " " : "") + (lineNumber ? lineNumber + ": " : "") + message); }; }); timer = setInterval(function() { console.log('Timeout!'); phantom.exit(1); }, 2000); }); </code></pre> <p>Html file (test.html):</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html class="x-border-box x-strict"&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;script type="text/javascript"&gt; var xhr = new XMLHttpRequest(); try { xhr.open('GET', 'test.txt', false); xhr.send(null); } catch (e) { console.log('cross origin?'); } console.log('status', xhr.status); console.log('response', xhr.responseText); &lt;/script&gt; &lt;/head&gt; &lt;body class="x-body"&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Chrome and --disable-web-security and ExtJs</strong></p> <p>I am actually using <code>--disable-web-security</code> as startup parameter to Google Chrome to run my webapp form the filesystem during development and it works there (no other Chrome processes must be running when starting Chrome). Chrome will display an alert message stating that you are using an unsupported option (yellow notification bar at the top). </p> <p>However, for Ext to work in a setup like this I required an additional patch to Ext.Connection in order to fix two issues: (1) xhr.status is always <code>0</code> when loading a file system resource. Ext does not consider this status code as <code>successful</code>. There is code dedicated to handling this when Ext is running in PhantomJs - that's why it should work there.</p> <p>(2) Chrome failed loading file system resources when the URL contains a query string. I override the Connection class to strip all url params when in filesystem mode.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload