简体   繁体   English

PHP $_SERVER['REQUEST_METHOD'] 总是用 AJAX 帖子返回 GET

[英]PHP $_SERVER['REQUEST_METHOD'] always returning GET with AJAX post

I am trying to set up an api for my website.我正在尝试为我的网站设置 api。 I would like to have a single page that does a different action depending on the request method.我想要一个页面,根据请求方法执行不同的操作。 ie. IE。 GET to get a resource, POST to create a new one, PUT to update and DELETE to delete. GET 获取资源,POST 创建新资源,PUT 更新,DELETE 删除。 I am testing the $_SERVER['REQUEST_METHOD'] variable to see which function I should run but for some reason it is always showing as get regardless of my ajax request.我正在测试$_SERVER['REQUEST_METHOD']变量以查看我应该运行哪个函数,但由于某种原因,无论我的 ajax 请求如何,它总是显示为 get。

Page 1第 1 页

var data = {
            store_id: id,
            reward: reward,
            disclaimer: disclaimer,
            period: period,
            active: active
        }

        //make request to save item
        $.ajax({
                type: "POST",
                url: 'http://' + '<?php echo $base_url; ?>' + 'api/birthday',
                data: data,
                dataType: 'json',
                //headers: {"X-HTTP-Method-Override": "POST"},
                success: function(obj){
                    console.log(obj);
                    $('#save-modal').dialog('open');
                },
                error: function(xhr, textStatus, errorThrown){
                    console.log(arguments);
                    console.log(xhr + '' + textStatus + ' ' + errorThrown);
                }
        });

Page 2第2页

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if( isset($_REQUEST['store_id']) &&
        isset($_REQUEST['reward']) &&
        isset($_REQUEST['disclaimer']) &&
        isset($_REQUEST['period']) &&
        isset($_REQUEST['active']) &&
        is_numeric($_REQUEST['active'])){
            exit(json_encode(update_birthday_reward(filter_var($_REQUEST['store_id'], FILTER_SANITIZE_NUMBER_INT), filter_var($_REQUEST['reward'], FILTER_SANITIZE_STRING), filter_var($_REQUEST['disclaimer'], FILTER_SANITIZE_STRING), filter_var($_REQUEST['period'], FILTER_SANITIZE_STRING), filter_var($_REQUEST['active'], FILTER_SANITIZE_NUMBER_INT))));   
    }else{
        exit(json_encode($_SERVER['REQUEST_METHOD']));
    }
}else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if(isset($_REQUEST['store_id'])){
        exit(json_encode(get_birthday_reward($_REQUEST['store_id'])));  
    }else{
        exit(json_encode($_SERVER['REQUEST_METHOD']));
    }
}

Looks like your request is being redirected to an actual PHP file, that's why you get POST first, which immediately translates to a GET request.看起来您的请求正在被重定向到一个实际的 PHP 文件,这就是您首先获得 POST 的原因,它立即转换为 GET 请求。

Try adding actual PHP file, like .../birthday/index.php , instead of .../birthday/ .尝试添加实际的 PHP 文件,例如.../birthday/index.php ,而不是.../birthday/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ajax发布到php返回$ _SERVER [&#39;REQUEST_METHOD&#39;] GET - ajax post to php returns $_SERVER['REQUEST_METHOD'] GET 在PHP中,iPhone POST请求始终被$ _SERVER [&#39;REQUEST_METHOD&#39;]视为GET - iPhone POST request is always seen as GET by $_SERVER['REQUEST_METHOD'] in PHP 为什么$ _SERVER [&#39;REQUEST_METHOD&#39;]总是GET? - Why is $_SERVER['REQUEST_METHOD'] always GET? $ _SERVER [&#39;REQUEST_METHOD&#39;]以及同时POST和GET - $_SERVER['REQUEST_METHOD'] and simultaneous POST and GET PHP REQUEST_METHOD POST 变成 GET - PHP REQUEST_METHOD POST turns into GET PHP在不使用$ _SERVER [&#39;REQUEST_METHOD&#39;]的情况下检测请求类型(GET,POST,PUT或DELETE) - PHP detecting request type (GET, POST, PUT or DELETE) without using $_SERVER['REQUEST_METHOD'] $_SERVER['REQUEST_METHOD'] 返回 GET insted POST - $_SERVER['REQUEST_METHOD'] return GET insted POST 表单操作是post,但回显$ _SERVER [&#39;REQUEST_METHOD&#39;]显示get - Form action is post but echoing $_SERVER['REQUEST_METHOD'] shows get 在表单上使用php和POST,但是request_method表示这是GET - Using php and POST on a form, but request_method says it is a GET In PHP Linux Azure Web App, $_SERVER['REQUEST_METHOD'] is always set to GET, even when POST, PATCH, DELETE etc requests are made through Postman - In PHP Linux Azure Web App, $_SERVER['REQUEST_METHOD'] is always set to GET, even when POST, PATCH, DELETE etc requests are made through Postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM