我正在运行node.js app;它工作正常,前端是angular.js
问题是我得到了:
XMLHttpRequest无法加载localhost:3000 / api / data_history。交叉源请求仅支持协议方案:http,data,chrome,chrome-extension,https,chrome-extension-resource。(匿名函数)@
来自此代码:
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();
});
答案 0 :(得分:0)
看起来你正在使用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");