Closed LostSenSS closed 10 years ago
Static only responds to GET
requests. To handle other request methods, you would need to add your own middleware and check for req.method === 'POST'
: https://github.com/gruntjs/grunt-contrib-connect#middleware
@shama i've tried your suggestion, but i can't get it to work. the server sends back a 405 status code.
code here: https://github.com/optimizely/marketing-website/blob/krush/oform/Gruntfile.js#L117
any ideas?
@kylerush You might need to unshift
your middleware instead of push
. My guess is another middleware is grabbing your POST
request first.
@shama that worked. thanks for the help.
I got a 404 response error when I send request using POST method, this is my task config?
connect: {
livereload: {
options: {
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,PATCH,POST,DELETE');
res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
return next();
});
return [
connect.static('.tmp'),
connect.static('.tmp/concat'),
connect().use('/bower_components'),
connect.static('app')
];
}
}
},
arr.unshift() doe's not accept a Function type arguments?
Oh sorry @luozhihua I misread the code. The unshift
usage there is correct, my bad.
But take a look at the array of middlewares you're returning vs the middlewares
array you're unshifting. That might be the problem. ;)
I'm getting 405 error as well. I'm on the latest version, v1.0.2. I have middleware setup set the headers to allow POST but I still get the error. This makes it very hard to test since I can test my changes locally.
server: {
options: {
base: ['<%= config.dev %>', '<%= config.app %>'],
open: true,
middleware: function (connect, options, middlewares) {
middlewares.unshift(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
return next();
});
return middlewares;
}
}
}
@mturnwall having the same problem, how did you fix it?
Hi! Why POST request can not work? Example : I send simple form on current page address. Output in Grunt console :
[D] server POST /build/views/profile.html 404 - - 1 ms
With GET all fine:D] server GET /build/views/profile.html?name=test 200 8960 - 1 ms
If I try to do the same on real server then all right.