In order to feed data from local files during AJAX call testing, the loading files can fail due to CORS. The error message from Chrome console could be like this.
XMLHttpRequest cannot load file:///clients.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
Chrome has a deprecated option to disable this CORS which is “–disable-web-security”.
If this option is specified when launching Chrome Browser on command line, CORS can be disabled.
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --incognito --disable-gpu --disable-web-security index.html
However, there is a corner case. If Chrome Browser is already up and running, then new Chrome process won’t be started, so there won’t be Chrome process using “–disable-web-security”. In this case, there could be two options.
- Close all tabs and terminate current running Chrome
- Run a new Chrome process
Since first option is strait forward, let’s take a look second option running a new Chrome process.
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --incognito --disable-gpu --disable-web-security --user-data-dir=/tmp/test index.html
As you can see there is a new option, “–user-data-dir”. When Chrome Browser runs, it uses the current user’s data. If “–user-data-dir” option is specified, then new user data directory will be created.