我正在尝试将一些数据从客户端发布到服务器端的脚本,但我仍然得到了这个 Eroor:
OPTIONS http://localhost/site/dbs.js Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin. jquery.js:9597
XMLHttpRequest cannot load http://localhost/site/dbs.js. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
server.js没有运行 node.js(路径 /wamp/www/site/server.js)
var app = require('express')();
var server = require('http').createServer(app);
var mysql = require('mysql');
var port = process.env.PORT || 8080;
server.listen(port);
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.get('/dbs.js', function(req, res) {
res.sendfile(__dirname + '/dbs.js');
});
在带有ajax()的index.html中,我调用将一些数据发布到 dbs.js:
$.ajax({
type: "POST",
url: " http://localhost:80/site/dbs.js",
data: "name="+username+"&pwd="password,
succes: function(ret)
{
if(ret==0)
;
}
});
dbs.js:
var name;
var pwd;
function DbConn()
{
var mydb = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'admin123',
database: 'users'
});
mydb.connect();
var query = ('select passwd from peer where username=' + username);
console.log(query);
connection.end(function(err) {
// The connection is terminated now
});
}
如果我更改 URL 中的某些内容,我会收到错误:404 - 未找到“dbs.js”所有源都在一个文件夹中(wamp/www/site/)。您认为有必要在 dbs.js 中添加一些 XML 标头吗