问题
This is a general JavaScript question but it pops up in this code which uses Splunk JavaScript SDK... the error is:
XMLHttpRequest cannot load file:///C:/proxy/services/auth/login?output_mode=json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. jquery.min.js:4
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="prettify.js"></script>
<script type="text/javascript" src="bootstrap.tabs.js"></script>
<script type="text/javascript" src="bootstrap.dropdown.js"></script>
<script type="text/javascript" src="jquery.placeholder.min.js"></script>
<script type="text/javascript" src="splunk.js"></script>
<script type="text/javascript" charset="utf-8">
console.log('a');
var http = new splunkjs.ProxyHttp("/proxy");
var service = new splunkjs.Service(http, {
username: "admin",
password: "Moravac123223",
scheme: "https",
host: "localhost",
port: "8089",
version: "5.0"
});
console.log('b');
// First, we log in
service.login(function(err, success) {
// We check for both errors in the connection as well
// as if the login itself failed.
if (err || !success) {
console.log("Login failure. Please check your server hostname and authentication credentials.");
done(err || "Login failed");
return;
}
// Now that we're logged in, let's get a listing of all the apps.
service.apps().fetch(function(err, apps) {
if (err) {
console.log("There was an error retrieving the list of applications:", err);
done(err);
return;
}
var appsList = apps.list();
console.log("Applications:");
for(var i = 0; i < appsList.length; i++) {
var app = appsList[i];
console.log(" App " + i + ": " + app.name);
}
done();
});
});
</script>
</body>
</html>
回答1:
I agree with Charlietfl. If you have apache and/or IIS, it should be a pretty quick test to validate that this fixed it.
If you are new to web-server's in general, have no fear they have applications like Xampp that setup your entire LAMP stack from one executable file.
Since you are authenticating into your own server, you may(not) run into this, but I have had trouble with running local RESTful applications, and pending what you are using on the backend I needed to do something like this: setHeader('Access-Control-Allow-Origin', '*'); to the header of my response.
Obviously you don't want something like this if you decide to create some sort of enterprise class web service, but for local testing, that generally gets around random CORS issues I have ran into.
Good luck with your web app!!
来源:https://stackoverflow.com/questions/32926421/cross-origin-requests-are-only-supported-for-protocol-schemes-http