简体   繁体   English

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

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

I am running my application with grunt task runner, it throws me following error: 我正在用咕task的任务运行程序运行应用程序,它引发以下错误:

POST http://localhost:8080/stub/cms/myalerts2.json 405 (Method Not Allowed) POST http:// localhost:8080 / stub / cms / myalerts2.json 405(不允许使用方法)

I have tried following things in my Gruntfile.js 我已经尝试在我的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;
                }   
} 
        } 
    },

Still its not allowing me to run. 仍然不允许我跑步。 Any help or suggestion will me most welcome. 任何帮助或建议我将非常欢迎。 Thanks in advance 提前致谢

So far i have tried following updation to my code, i am able to get rid of Method not Allowed 405 Error but currently i am getting "Failed to load resource: the server responded with a status of 404 (Not Found)" i have modified my code as below:- 到目前为止,我已经尝试更新代码,可以摆脱方法不允许的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();
                              }];
                        }

                } 
            } 
        },

Any suggestion will be appreciated. 任何建议将不胜感激。

After couple of irritating days finally my code works for me. 经过几天的烦恼,终于我的代码为我工作了。 Look into my code below:- 查看下面的代码:

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

                } 
            } 
        },

see the star marked line ie if (req.method.toUpperCase() == 'POST') req.method='GET'; 看到星号标记的行,即if (req.method.toUpperCase() == 'POST') req.method='GET'; i done this trick and its worked for me. 我做了这个把戏,它为我工作。 This aricle also helps to me https://github.com/gruntjs/grunt-contrib-connect#middleware 该文章也对我有帮助https://github.com/gruntjs/grunt-contrib-connect#middleware

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM