我正在使用grunt任务运行程序运行我的应用程序,它会抛出以下错误:
POST http://localhost:8080/stub/cms/myalerts2.json 405(不允许使用方法)
我在Gruntfile.js中尝试过以下内容
connect: {
server: {
options: {
keepalive: true,
port: 8001,
protocol: 'http',
hostname: '*',
base: 'dis',
directory: 'dis',
open: {
target: 'http://localhost:8001/mydemo.html',
appname: 'open'
},
middleware: function(connect, options, middlewares) {
middlewares.unshift(function(req,res,next){
res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
});
return middlewares;
}
}
}
},
仍然不允许我跑。我非常欢迎任何帮助或建议。提前致谢
答案 0 :(得分:0)
到目前为止,我已尝试对我的代码进行更新后,我能够摆脱方法不允许405错误,但目前我收到“无法加载资源:服务器响应状态为404(未找到)” 我修改了我的代码如下: -
connect: {
server: {
options: {
keepalive: true,
port: 8001,
protocol: 'http',
hostname: '*',
base: 'dist',
directory: 'dist',
open: {
target: 'http://localhost:8001/myDemo.html',
appname: 'open'
},
middleware: function(connect, options, next) {
return [
function(req, res, next) {
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
next();
}];
}
}
}
},
任何建议都将受到赞赏。
答案 1 :(得分:0)
经过几天的烦恼,我的代码最终适合我。请查看下面的代码: -
connect: {
server: {
options: {
keepalive: true,
port: 8001,
protocol: 'http',
hostname: '*',
directory: 'dist',
open: {
target: 'http://localhost:8001/myDemo.html',
},
middleware: function(connect, options, middlewares) {
middlewares.unshift(function(req, res, next) {
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
**if (req.method.toUpperCase() == 'POST') req.method='GET';**
return next();
});
return middlewares;
}
}
}
},
看到明星标记的线,即if (req.method.toUpperCase() == 'POST') req.method='GET';
我完成了这个技巧,它对我有效。
这篇文章也对我https://github.com/gruntjs/grunt-contrib-connect#middleware