获取错误 POST http://localhost:8080/stub/cms/myalerts2.json 405(方法不允许)

Getting Error POST http://localhost:8080/stub/cms/myalerts2.json 405 (Method Not Allowed)

我运行使用 g运行t 任务 运行 连接我的应用程序,它抛出以下错误:

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;
                }   
} 
        } 
    },

仍然不允许我 运行。我非常欢迎任何帮助或建议。提前致谢

到目前为止,我已经尝试更新我的代码,我能够摆脱 Method not Allowed 405 Error 但目前我得到 "Failed to load resource: the server responded with a status of 404 (Not Found)" 我修改了我的代码如下:-

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();
                              }];
                        }

                } 
            } 
        },

如有任何建议,我们将不胜感激。

经过烦人的几天后,我的代码终于对我有用了。在下面查看我的代码:-

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