ajax - 使用 Postman 时 Laravel 无法将 Request 识别为 Ajax
问题描述
当我从 Postman 应用程序向 Laravel 应用程序发出请求时,
$request->ajax()
不返回 TRUE。
我也在Accept: application/json
邮递员中发送标题,仍然没有运气。
邮递员请求截图:
代码截图
解决方案
添加标题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
.
推荐阅读
- react-native - fetch 在本机反应中无法在真实设备上运行
- excel - 将最近 5 天工作簿的单元格值汇总到单个工作簿中
- if-statement - Crystal 2008 中此处需要一个字符串(If Else 语句)
- security - SSL 客户端问候重置
- php - PHP - 将拍卖结束日期设置为在工作日结束
- javascript - 在不知道“data-”之后的内容的情况下获取 data-* 属性的值
- android - 清单合并失败:属性 application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory)
- flask-assets - 烧瓶资产 append_path() catch-22
- javascript - TypeError:无法读取未定义 jQuery 的属性“顶部”
- php - PHP 和 Ruby 上的相同代码给出不同的结果