What I'm trying to do
Post from a local html/javascript website to an online PHP file.
Problem
When I attempt to utilise my below code, I keep receiving the below mentioned error.
Background:
This website is intended to run locally. As it's each user's choice which browser they use, I'm looking to discover an approach which could solve the above, without forcing browser policy modification onto each user.
Is this possible, if so how?
Javascript code:
$.ajax({
type: 'POST',
url: 'http://example.com/test.php',
crossDomain: true,
data: "my_request_is=foo",
dataType: 'json',
success: function(responseData, textStatus, jqXHR)
{
console.log(responseData);
},
error: function (responseData, textStatus, errorThrown)
{
console.warn(responseData, textStatus, errorThrown);
alert('CORS failed - ' + textStatus);
}
});
Php code (test.php):
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
echo json_encode(array("your_request_was" => $_POST['my_request_is']));
?>
Error:
Due to chrome security settings, to make an XMLHttpRequest you need a valid origin and a valid destination.
The destination is a valid domain, and because you have Access-Control-Allow-Origin enabled, you can make a request from different origin.
But the origin is not a valid domain, because you are doing a request from a local page.
The origin must be a valid host/identifier (domain for a website, identifier for a browser extension, application id for a webview embedded into an application).
You have these option to solve the issue:
disable browser security settings: sample for chrome:
chromium-browser --disable-web-security --user-data-dir
check this answer, for how to use: https://stackoverflow.com/a/3177718/5081328
embed your page into an application and use a webview
Create a browser extension to load your page
By default cross-origin requests are not allowed at chrome, chromium from
file:
protocol. You can close open instances of chrome, chromium and launch with--disable-web-security
flagor launch with existing instances open by setting a different
user-data-dir
Not at chrome, chromium without modifying default settings; or creating an chromium extension or app to perform network requests - where too, proper
permissions
need to be set.The restriction is there for a purpose. One of the several security issues is that user at a local computer could, possibly unknowingly, upload a listing of all of the files in one or more directories on their computer, and potentially the directories themselves, without necessarily being aware of that fact, see
How FileReader.readAsText in HTML5 File API works?
which; note, could also occur without flags being set. Or, a requested script could perform actions to read or write to local filesystem without user necessarily being aware of their local filesystem being accessed by an external script.
Though, with restrictions in place, the user has to perform an affirmative action to disable default settings restricting access to local file system from
file:
protocol, and restricting local file system to fetch resources fromnull
origin
to a different origin.As noted by @StefanoBalzarotti
which should be taken into consideration as to the reasons why browser developers would implement such a default limitation on cross origin request.
Used sparingly, and with awareness of the significance of the flags, the flags
--disable-web-security
and--allow-file-access-from-files
, seeHow do I make the Google Chrome flag “--allow-file-access-from-files” permanent?
List of Chromium Command Line Switches
are designed for local web development, not as a workaround for a local application which requires resources from a web application.
"testing" term in description of flags should provide emphasis of the usage of the flags. The flags are not designed for production usage.
Alternatives,
Create a chromium extension to perform network tasks;
Create a chrome app to perform network tasks
where either requires proper
permissions
settings atmanifest.json
.