我的Visual Studio两个项目1中的一个解决方案是mvc4项目,而2是node js项目。 我试图获取节点js项目中对象的数据,并且在我的mvc项目的前端页面中调用此函数... 例如 第一个项目端口是http://localhost:1011-这是mvc项目 第一个项目端口是http://localhost:3215-这是节点js项目
在我的点击按钮事件前端中进行了jquery调用
从原点“ http://localhost:3215/?Id=8014”到“ http://localhost:1011”的XMLHttpRequest的访问已被CORS策略阻止:所请求的资源上没有“ Access-Control-Allow-Origin”标头。请建议我如何解决此错误.....
jquery:-
function abctest(Id) {
alert(1);
$.ajax({
type: 'GET',
url: 'http://localhost:3215',
timeout: 3000,
dataType: 'application/json',
data: {Id:Id},
success: function (data) {
//show content
alert('Success!')
},
error: function (Error) {
alert(Error);
}
});
}
server.js:-
var express = require('express');
let $ = jQuery = require('jquery');
var app = express();
var libraryData;
var cust_dynamicdata;
global.abcdObj = { projectDir: __dirname };
var server = app.listen(3215, function () {
console.log('Server is running..');
});
app.get('/', function (req, response) {
libraryData = require(__dirname + '/build/Release/mylib');
cust_dynamicdata= new libraryData.abc(__dirname + "/");
response.send('test');
});
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:1011');
// 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();
});