PHP无法识别通过AJAX发送的PUT / POST请求

时间:2019-07-02 12:15:33

标签: php jquery ajax

我正在尝试通过jQuery将POST和PUT请求发送到PHP应用程序。 index.php是我的控制器,它检查$ _SERVER [“ REQUESTED_METHOD”]并用作调度程序。例如:如果发送了GET请求,则我的index.php会发回主页的渲染视图。

现在,我想实现更多的请求方法,例如PUT和POST。但是它们都无法正常工作。我的index.php始终表现得像是发送了GET。

当我通过POSTMAN发送请求时,index.php响应正确。通过纯HTML(method =“ post” action =“ index.php”)的POST请求也可以正常工作。

在Chrome DevTools中,当我通过jQuery发送请求时,显示了正确的请求标头。但是在$ _SERVER []中显示了请求方法“ GET”。

index.php

<?php

// CONSTANTS
define('ROOT', str_replace("public", "", $_SERVER['DOCUMENT_ROOT']));

// INCLUDES
require_once(ROOT . "core/Authenticator.php");
require_once(ROOT . "model/User.php");
require_once(ROOT . "model/ShoppingList.php");
require_once(ROOT . "views/Lists.php");



if(Authenticator::checkAuthentication()) {

    $method = $_SERVER['REQUEST_METHOD'];
    Utility::debug($method);

    switch ($method) {
        case 'GET':
            Lists::render();
            break;
        case 'POST':
            Print("POST");
            break;
        case 'PUT':
            Print("PUT");
            break;
        case 'DELETE':
            print("DELETE");
            break;
        default:
            print("DEFAULT");
            break;
    }

} else {

    header("Location: login.php");
    die();

}

script.js

/* global $*/

$(document).ready(function () {
    $('input[type="checkbox"]').click(processArticle);
});


function processArticle() {

    let data = {};
    data.article = "article";
    data.id = $(this).attr('id');

    $.ajax({
        method: "PUT",
        url: "index.php",
        data: data
    });
};

0 个答案:

没有答案
相关问题