发布网站后,使用AngularJS发布表单返回405代码

时间:2017-09-24 10:45:58

标签: javascript angularjs post methods http-status-code-405

当我将表格中的数据发布到我收到的服务器时,我遇到了问题:

angular.js:14642 Possibly unhandled rejection: {"data":"<html>\n<head><title>405 Not Allowed</title></head>\n<body bgcolor=\"white\">\n<center><h1>405 Not Allowed</h1></center>\n</body>\n</html>","status":405,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/messages","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"Method Not Allowed"}

在localhost上一切正常,但发布后我在帖子上收到此错误。我的网站发布在Github页面上,域名是namecheap。

这是我的路线代码:

module.exports = (function() {
'use strict';
var router = require('express').Router();
var Message = require('./angular/models/messageModel');

router.post('/messages', function(req,res,next){
    var message = new Message();                                    
    message.name = req.body.name;
    message.email = req.body.email;
    message.subject = req.body.subject;
    message.message = req.body.message;

    if(req.body.name == null || req.body.name == "" || req.body.email == null || req.body.email == "" || req.body.subject == null || req.body.subject == "" || req.body.message == null || req.body.message == ""){
    res.json({success: false, message: "Please fill up the required fields!" });}
    if(req.body.message.length<8){
            res.json({success: false, message: "Your message is too short!"});

    } else {
        message.save(function(err){
            if(err){
                throw err;
            } else { 
                res.json({success: true, message: "Message sent!"}); 
                }
        });
    }

});

return router;
})();

这是我的要求:

$http({
            method: 'POST',
            url: '/messages',
            headers: { "Content-Type": "application/json" },
            data: app.contactData
            })

任何帮助将不胜感激!

编辑:我添加了响应标头,但没有任何更改

app.use(function (req, res, next) {

res.setHeader('Access-Control-Allow-Origin', 'http://www.stundji.info');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT,');
res.setHeader('Access-Control-Allow-Headers', 'application/json');
res.setHeader('Access-Control-Allow-Credentials', false);
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

next();
});

1 个答案:

答案 0 :(得分:0)

问题已经解决。

问题来自GitHub Pages。我切换到Heroku,一切都恢复正常。我对每个人的建议是不要在GitHub页面上托管他们的网站,似乎它对某些请求有限制。