首页 > 解决方案 > 发布请求在邮递员中工作,但不在 ajax 中,给出 405 方法未找到错误

问题描述

我在 xampp 上使用带有 apache 和 mysql 的 localhost 服务器。我正在实现一个登录功能,我的 api 中存在 php 代码。当我使用 POSTMAN 发出 POST 请求时,结果正确,但是当我发出 ajax 请求时,我收到 405 method not allowed 错误。

我认为标题设置得当,但我仍然不知道为什么会出现错误。如果有人能指出为什么会出现错误并在可能的情况下提供解决方案代码,我将不胜感激。

这是我的js代码

```var sendRequest = function(formData) {

    var request = new XMLHttpRequest();

    request.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                response = JSON.parse(this.responseText);
                console.log("request sent succesfully..");
                return response;                  
                    }
            if (this.readyState == 4 && this.status == 401) {
                response = false;
                return response;
                    }

            }; 
    request.open("POST","../api/objects/user/login.php",true);
    request.setRequestHeader("content-type", "application/json");
    request.send(formData);
};```

这是我的 php 代码。

<?php
// required headers
header("Access-Control-Allow-Origin: 
http://http://127.0.0.1:8080/login.html" );
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow- 
Headers, Authorization, X-Requested-With");

include_once '../../config/database.php';
include_once 'user.php';

//files for json web token
include_once '../../config/core.php';
include_once '../../libs/php-jwt-master/src/BeforeValidException.php';
include_once '../../libs/php-jwt-master/src/ExpiredException.php';
include_once '../../libs/php-jwt- 
master/src/SignatureInvalidException.php';
include_once '../../libs/php-jwt-master/src/JWT.php';
use \Firebase\JWT\JWT;


$db = new Database();
$conn = $db->getConnection();

$user = new User($conn);

$data = json_decode(file_get_contents("php://input"));

$user->email = $data->email;

$email_exists = $user->emailExists();

if($email_exists && password_verify($data->password, $user->password))
{
$token = array(
"iss" => $iss,
"aud" => $aud,
"iat" => $iat,
"nbf" => $nbf,
"data" => array(
 "id" => $user->id,
 "firstname" => $user->firstname,
 "lastname" => $user->lastname,
 "email" => $user->email
 )
 );

http_response_code(200);

//generate JWT
$jwt = JWT::encode($token, $key);

echo json_encode(
 array(
     "message" => "Succesful Login",
     "jwt" => $jwt
)
);

}

else {
http_response_code(401);

echo json_encode(array("message" => "Login failed"));
}
?>  

邮递员以 json 格式发送电子邮件和密码的结果

{ "message": "Succesful Login", "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJhdWQiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJpYXQiOjEzNTY5OTk1MjQsIm5iZiI6MTM1NzAwMDAwMCwiZGF0YSI6eyJpZCI6IjIiLCJmaXJzdG5hbWUiOiJwcmFuYXYiLCJsYXN0bmFtZSI6ImFyb3JhIiwiZW1haWwiOiJhcm9yYS5wcmFuYXYuMjAwMEBnbWFpbC5jb20ifX0.npQWi4HP_QlGmmlkaPve3MBdn8u_yD_MxplUSKebNOY" }

浏览器显示的错误

login.js:25 POST http://127.0.0.1:8080/api/objects/user/login.php 405 (Method Not Allowed)

标签: php

解决方案


推荐阅读