简体   繁体   中英

Expressjs 405 POST method not allowed

Route perfectly works well from POSTMAN chrome extension, with Angular it doesn't.

Well here goes my Express js code :

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 :

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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