简体   繁体   English

Expressjs 405 POST方法不允许

[英]Expressjs 405 POST method not allowed

Route perfectly works well from POSTMAN chrome extension, with Angular it doesn't. 路线完美适用于POSTMAN chrome扩展,而Angular却没有。

Well here goes my Express js code : 那么这里是我的Express js代码

var express = require('express');
var router = express.Router();
var app = express();
var bodyParser = require('body-parser')
var routes = require('./routes');
var connection  = require('express-myconnection');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

/** Serve our app on root path */
app.use('/', express.static('app'));

/** Login API */
app.post('/login', routes.login);

And here goes Angular code : 这里是Angular代码

$http({
    method: 'POST',
    url: apiUrl + 'login',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded' // necessary for expressjs
    },
    transformRequest: function(obj) {
        var str = [];
        for (var p in obj) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
        return str.join("&");
    },
    data: user
});

Not sure whats wrong! 不确定什么是错的! This is what I get : 这就是我得到的:

在此输入图像描述

Try inspecting the headers and content for both requests, there's bound to be a difference between the two. 尝试检查两个请求的标题和内容,两者之间必然存在差异。 Your response's Allow header clearly does not include POST, so there might be some CORS issue going on there. 您的响应的Allow标题显然不包含POST,因此可能会出现一些CORS问题。

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

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