简体   繁体   中英

How to resolve a HTTP/1.1 POST 405 Method Not allowed error

I'm building a webpage and am currently stuck on the login page section of the code. Whenever I try to login I keep getting this error warning: "HTTP/1.1 POST /check-login" - 405 Method Not Allowed. I've reviewed my code a few times and compared it to the lecture videos I'm following but nothing I've tried so far seems to let me get past this error. I'm fairly new to teaching myself to code so the answer may be really simple I just can't see it. This project works across several programing languages and is on several different pages. Most of the code is in Python and HTML with one page in java script. I think that the java script page may be the cause of the hold up since that is where the the check-login code is mostly set up. If anyone can help me identify where the problem is and how to correct it I'd appreciate the help. These are the sections of code that I believe relate to the problem that's occurring.

1.

  $(document).on("submit", "#login-form", function(e){
        e.preventDefault();

        var form = $(this).serialize();
        $.ajax({
            url: '/check-login',
            type: 'POST',
            data: form,
            success: function(res){
                if(res == "error"){
                    alert("Could not log in");
                }
                else{
                    console.log("Login as ", res);
                    window.location.href = "/";
                }
            }
        });
    });
import pymongo
from pymongo import MongoClient
import bcrypt


class LoginModel:
    def __init__(self):
        self.client = MongoClient()
        self.db = self.client.codewizard
        self.Users = self.db.users

    def check_user(self, data):
        user = self.Users.find_one({"username": data.username})

        if user:
            if bcrypt.checkpw(data.password.encode(), user["password"]):
                return user
            else:
                return False
        else:
            return False
  1. class CheckLogin: def Post(self): data = web.input() login = LoginModel.LoginModel() isCorrect = login.check_user(data)

     if isCorrect: return isCorrect return "error"

Try adding a trailing slash to the URL in AJAX request:

$.ajax({
  url: '/check-login/',
  ...

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