gpt4 book ai didi

javascript - 由于交叉问题,nodejs无法加载URL

转载 作者:太空宇宙 更新时间:2023-11-04 02:23:01 24 4
gpt4 key购买 nike

我正在运行node.js应用程序;它工作正常,前端使用 angular.js
问题是我得到:

XMLHttpRequest cannot load localhost:3000/api/data_history. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.(anonymous function) @

从此代码:

app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', properties.clientHost);

// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);

// Pass to next layer of middleware
next();
});

最佳答案

看起来你正在使用express....而不是手动执行,使用这个中间件怎么样:https://www.npmjs.com/package/cors有时这是一个简单的解决方案

var sqlite3    = require('sqlite3').verbose();
var db = new sqlite3.Database('data/demodb02');
var express = require('express');
var cors = require('cors');
var app = express();
var bodyParser = require('body-parser');
var fs = require('fs');


app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


db.serialize(function() {
//db.run("CREATE TABLE IF NOT EXISTS counts (key TEXT, value INTEGER)");
db.run("CREATE TABLE IF NOT EXISTS sensor (id INT PRIMARY KEY, sensor_type integer, data real)");
db.run("CREATE TABLE IF NOT EXISTS location (longitude real NOT NULL, latitude real NOT NULL, sensor_id integer, PRIMARY KEY (longitude, latitude), CONSTRAINT id FOREIGN KEY (sensor_id) REFERENCES sensor(id))");
db.run("CREATE TABLE IF NOT EXISTS data_live (sensor_id INTEGER, sensor_data REAL, time time, CONSTRAINT id FOREIGN KEY (sensor_id) references sensor(id) CONSTRAINT data FOREIGN KEY (sensor_data) references sensor(data))");
db.run("CREATE TABLE IF NOT EXISTS data_history (sensor_id INTEGER, sensor_data REAL, time time, date date, CONSTRAINT id FOREIGN KEY (sensor_id) references sensor(id) CONSTRAINT id CONSTRAINT data FOREIGN KEY (sensor_data) references sensor(data))");
db.run("CREATE TABLE IF NOT EXISTS client (login varchar(50), password varchar(50), user_id integer, sensor_id integer, CONSTRAINT id FOREIGN KEY (sensor_id) references sensor(id))");


// test //
//db.run("INSERT INTO counts (key, value) VALUES (?, ?)", "counter", 0);
db.run("INSERT INTO sensor (id, sensor_type, data) VALUES (?, ?, ?)", 1, 4, 8);
db.run("INSERT INTO sensor (id, sensor_type, data) VALUES (?, ?, ?)", 2, 9, 4);
db.run("INSERT INTO sensor (id, sensor_type, data) VALUES (?, ?, ?)", 3, 7, 1);
db.run("INSERT INTO sensor (id, sensor_type, data) VALUES (?, ?, ?)", 4, 1, 15);
db.run("INSERT INTO location (longitude, latitude, sensor_id) VALUES (?, ?, ?)", 50, 45, 1);
db.run("INSERT INTO data_live (sensor_id, sensor_data, time) VALUES (?, ?, ?)", 1, 8, "08:44");
db.run("INSERT INTO data_history (sensor_id, sensor_data, time, date) VALUES (?, ?, ?, ?)", 1, 8, "08:45", "12/05/2015");
db.run("INSERT INTO client (login, password, user_id, sensor_id) VALUES (?, ?, ?, ?)", "user", "user", 1, 1);

// fin test //
});


// dynamically include routes (Controller)//
fs.readdirSync('./controllers').forEach(function (file) {
if(file.substr(-3) == '.js') {
route = require('./controllers/' + file);
route.controller(app, db);
}
});
//include the rules engine files //

/*s.readdirSync('./rules').forEach(function (file) {
if(file.substr(-3) == '.js') {
route = require('./rules/' + file);
//route.controller(app, db);
}
});*/


app.listen(3000)

console.log("Submit GET or POST to http://localhost:3000/api");

关于javascript - 由于交叉问题,nodejs无法加载URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com