Home  >  Article  >  PHP Framework  >  How to solve thinkphp request type error

How to solve thinkphp request type error

PHPz
PHPzOriginal
2023-04-07 09:28:25807browse

When developing using the ThinkPHP framework, we often encounter the problem of request type errors. For example, when we submit a form using the POST method, a "request type error" prompt appears. So, why does this problem occur? How to solve it?

First, let’s understand the concept of request type. In the HTTP protocol, there are two request methods: GET and POST. The GET request concatenates the data behind the URL and sends it to the server, while the POST request puts the request data in the message body of the HTTP request and sends it to the server. At the same time, there are methods such as PUT and DELETE. In the ThinkPHP framework, we can obtain the request method through $_SERVER['REQUEST_METHOD'].

If we get a "request type error" prompt when we use the POST method to request, then the possible reason is that the server cannot obtain the POST parameters when we use the POST method to request. Here are two common situations:

1. csrf_token is not added to the form

In ThinkPHP, in order to prevent form forgery attacks, we must add csrf_token to the form to verify the source of the form. legality. If it is not added to the form, a "Request Type Error" will appear. The solution is to add the csrf_token tag to the form, as follows:

<form method="post">
    <!-- 在这里加入csrf_token标志 -->
    <?php echo token();?>
    <input type="text" name="username" />
    <input type="password" name="password" />
    <button type="submit">提交</button>
</form>

2. Not turning off CSRF defense

If we turn on global CSR defense, we do not turn off csrf in the controller Defense, the "Request Type Wrong" prompt will appear. The solution is to turn off csrf defense in the controller. The code is as follows:

class IndexController extends Controller
{
    //关闭csrf防御
    protected $middleware = [
        \think\middleware\AllowCrossDomain::class,
        \think\middleware\CheckRequestCache::class,
        \think\middleware\SendFile::class,
        \think\middleware\ValidateRequest::class => [
            //关闭csrf防御
            'except' => ['login']
        ],
    ];
    
    //login方法
    public functtion login()
    {
        //...
    }
}

In addition to the above two situations, there is another possibility that we have a request construction error when making an AJAX request. Specifically, when we use the $.post() or $.ajax() method to request, we do not correctly write the dataType, contentType and other parameters, resulting in an incorrect request type.

To sum up, if we encounter a request type error message when using ThinkPHP, we should first check whether the above three situations occur and solve them accordingly.

The above is the detailed content of How to solve thinkphp request type error. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn