不允许POST请求 - 405 Not Allowed - nginx,即使包含头文件也是如此

deb*_*eka 43 nginx cross-domain node.js express nodemailer

我在尝试在我的应用程序中执行POST请求时遇到问题并且我搜索了很多,但是我找不到解决方案.

所以,我有一个nodeJS应用程序和一个网站,我正在尝试使用此站点的表单进行POST请求,但我总是这样做:

在此输入图像描述

在控制台中我看到:

    Uncaught TypeError: Cannot read property 'value' of null 
Post "http://name.github.io/APP-example/file.html " not allowed
Run Code Online (Sandbox Code Playgroud)

在这行代码中:

file.html:

<form id="add_Emails" method ="POST" action="">

    <textarea rows="5" cols="50" name="email">Put the emails here...
    </textarea>

        <p>
        <INPUT type="submit" onclick="sendInvitation()" name='sendInvitationButton' value ='Send Invitation'/>
        </p>


</form>

<script src="scripts/file.js"></script>
Run Code Online (Sandbox Code Playgroud)

file.js:

function sendInvitation(){

    var teammateEmail= document.getElementById("email").value;
Run Code Online (Sandbox Code Playgroud)

我阅读了很多帖子和跨域文档,但它没有用.研究来源1:http://enable-cors.org/server.html 研究来源2:http://www.w3.org/TR/2013/CR-cors-20130129/#http-access-control-max-年龄

我现在在做什么:

我正在尝试从我的服务器的不同域POST:

POST REQUEST: http://name.github.io/APP-example/file.html,github repository

POST LISTENER:" http://xxx.xxx.x.xx:9000/email,server localhost(x->我的ip地址)

所以,我在其他文件中遇到了同样的问题,但是我修复了它将这段代码放在每条路径的开头:

var express = require('express');
var sha1 = require('sha1'); 

var router = express.Router(); 
var sessionOBJ = require('./session');

var teams = {} 
var teamPlayers = []

router.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT, GET,POST");
  next();
 });
Run Code Online (Sandbox Code Playgroud)

我修好了它.

现在,我遇到了同样的问题,但在这个文件中唯一的区别是我处理SMTP和电子邮件,所以我发布了一封电子邮件并发送电子邮件到我在POST请求中收到的这封电子邮件.

这个代码与POSTMAN完全一致,所以,当我用POSTMAN测试时,它可以工作,我可以发布.

我在下面列出了这个代码,而不是我展示的第一个代码,但是它没有起作用:

router.all('*', function(req, res, next){
            res.header("Access-Control-Allow-Origin", "*")
            res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
            res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept")
            res.header("Access-Control-Max-Age", "1728000")
            next();
        });
Run Code Online (Sandbox Code Playgroud)

有人知道如何解决它吗?

谢谢.

Bas*_*kar 49

这个配置到你的nginx.conf应该会对你有所帮助.

https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;

    # To allow POST on static pages
    error_page  405     =200 $uri;

    # ...
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个黑客,你也没有努力解释它. (79认同)
  • 哇,它有效.但它真的与脏黑客无法区分. (5认同)
  • 似乎他只是将 405 改写为 200 - 让它看起来像是有效的。很高兴知道这是否有效。即,它会返回静态资源/ (3认同)
  • 在这些文章中可以找到一些进一步的解释:[Serving Static Content Via POST From Nginx](http://invalidlogic.com/2011/04/12/serving-static-content-via-post-from-nginx/),以及 [Nginx:在使用静态 POST 服务时修复“405 Not Allowed”](http://leandroardissone.com/post/19690882654/nginx-405-not-allowed) (2认同)

小智 5

这是到目标服务器的真正代理重定向。

server {
  listen          80;
  server_name     localhost;
location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
    proxy_pass http://xx.xxx.xxx.xxx/;
    proxy_redirect off;
    proxy_set_header Host $host;

  }
}
Run Code Online (Sandbox Code Playgroud)