首页 > 解决方案 > 使用 Postman 时 Laravel 无法将 Request 识别为 Ajax

问题描述

当我从 Postman 应用程序向 Laravel 应用程序发出请求时,

$request->ajax()不返回 TRUE。

我也在Accept: application/json邮递员中发送标题,仍然没有运气。

邮递员请求截图

邮递员请求截图

代码截图

代码截图

标签: ajaxlaravelrestapipostman

解决方案


添加标题X-Requested-With: XMLHttpRequest

说明

如果您跟踪该方法->ajax(),它将通过以下功能:

    /**
     * Returns true if the request is a XMLHttpRequest.
     *
     * It works if your JavaScript library sets an X-Requested-With HTTP header.
     * It is known to work with common JavaScript frameworks:
     *
     * @see https://wikipedia.org/wiki/List_of_Ajax_frameworks#JavaScript
     *
     * @return bool true if the request is an XMLHttpRequest, false otherwise
     */
    public function isXmlHttpRequest()
    {
        return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
    }

添加标题X-Requested-With: XMLHttpRequest,然后$request->ajax()将返回TRUE.


推荐阅读