解析服务器-无法从远程计算机上的数据库获取数据



我使用的是Parse Server,我有在C#和Swift上开发的软件,可以使用适当的SKU将数据从该服务器远程获取到每种编程语言,没有任何问题。

但现在我需要使用浏览器获取数据,而从远程计算机获取数据时遇到了问题。我可以在安装Parse服务器的计算机上使用localhost来获取它。

我正在使用javascript的parse sdk。

这是我的代码(index.html):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Teste</title>
<script src="https://unpkg.com/parse/dist/parse.min.js"></script>
<script>
function executaScript() {
console.log("Script")
Parse.initialize("HG", "HGJSKey", "HGMasterKey");
Parse.serverURL = "http://localhost:1337/parse";
var etapasInternacao = Parse.Object.extend("EtapaInternacao");
var innerQuery = new Parse.Query(etapasInternacao);
innerQuery.notEqualTo("objectId", "o0rudDQzng");
var interns = Parse.Object.extend("Internacao");
var query = new Parse.Query(interns);
query.include("paciente");
query.include("etapa");
query.ascending("numeroQuarto");
query.matchesQuery("etapa", innerQuery);
query.find().then((resultado) => {
this.internacoes = []
for(var indice in resultado) {
console.log(resultado[indice].get("paciente").get("nomeCompleto"))
}
}, (error) =>  {
// error is an instance of parse.error.
alert(error.message)
});
}
document.onload = executaScript()
</script>
</head>
<body>
<h1>Script</h1>
</body>
</html>

这是解析服务器的inicial文件(index.js)

// Example express application adding the parse-server module to expose Parse
// compatible API routes.
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
const resolve = require('path').resolve;
var api = new ParseServer({
databaseURI: 'mongodb://localhost:27017/HGDB',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'HG',
masterKey: process.env.MASTER_KEY || 'HGMasterKey', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't forget to change to https if needed
clientKey: 'HGKey',
javascriptKey: 'HGJSKey',
restAPIKey: 'HGRestKey',
dotNetKey: 'HGDotNetKey',
fileKey: 'HGFileKey',
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
}
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
var app = express();
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('I dream of being a website.  Please star the parse-server repo on GitHub!');
});
// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

以下是我从浏览器收到的错误:

谷歌浏览器:

POSThttp://localhost:1337/parse/classes/Internacaonet::ERR_CONNECTION_REFUSED

Mozila Firefox:

阻止跨来源请求:同源策略不允许读取位于的远程资源http://localhost:1337/parse/classes/Internacao.(原因:CORS请求未成功)。

我已经下载了cors(),并在应用程序声明后使用了它,但它仍然不起作用。

[...]
var app = express();
app.use(cors());
[...]

我尝试过创建路线和cors选项,但没有任何变化。

我更改了这行

Parse.serverURL = "http://localhost:1337/parse";

Parse.serverURL = "http://[SERVER-IP]:1337/parse";

现在它正在发挥作用。

相关内容

  • 没有找到相关文章

最新更新