Laravel 请求:判断是否是 Ajax 请求

                    <div class="toc-wraper ui  card column author-box grid pt-2" style="display:none">
                        <a href="javascript:void(0);" class="close-toc pull-right" style="width: 20px;position: absolute;right: 7px;top: 3px;margin-bottom: 20px;">
                            <i class="icon remove text-mute "></i>
                        </a>
                        <div id="toc" class="tocify">
                        <ul id="tocify-header0" class="tocify-header nav nav-list"><li class="tocify-item active" data-unique="5dc99f"><a>问题</a></li></ul><ul id="tocify-header1" class="tocify-header nav nav-list"><li class="tocify-item" data-unique="29f56d"><a>方法一、ajax () 函数</a></li><ul class="tocify-subheader nav nav-list" data-tag="3"><li class="tocify-item" data-unique="165dcc"><a>直接访问的话:</a></li><li class="tocify-item" data-unique="43ddcd"><a>使用 Ajax 访问:</a></li></ul></ul><ul id="tocify-header2" class="tocify-header nav nav-list"><li class="tocify-item" data-unique="f8ed16"><a>方法二、expectsJson ()</a></li></ul></div></div><div name="5dc99f" data-unique="5dc99f"></div><h2 id="5dc99f">问题<a href="#5dc99f" class="anchorific" style="display: none;">#</a></h2>

如何判断请求是来自 Ajax 或者是正常的 URL 访问?

方法一、ajax() 函数

routes/web.php

.
.
.
Route::get('test-ajax', function () {
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">ajax</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token single-quoted-string string">'来自 AJAX 请求'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token single-quoted-string string">'正常访问'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

});

直接访问的话:#

判断是否是 Ajax 请求

使用 Ajax 访问:

此刻打开 Chrome 控制台,贴入以下内容:

$.get("http://homestead.test/test-ajax", function(data) {
    console.log(data);
});

注意:记得把 homestead.test 换为你自己的域名。

判断是否是 Ajax 请求

方法二、expectsJson()

Route::get('test-ajax', function () {
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">expectsJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token single-quoted-string string">'来自 AJAX 请求'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token single-quoted-string string">'正常访问'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

});

expectsJson() 使用方法与 ajax() 相近,不过查看源代码后还是能看到区别:

// 判断客户端是否期待返回 JSON 数据
public function expectsJson()
{
    return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
}

// 判断客户端是否「请求返回 JSON 格式」的数据
public function wantsJson()
{
$acceptable = $this->getAcceptableContentTypes();

<span class="token keyword">return</span> <span class="token keyword">isset</span><span class="token punctuation">(</span><span class="token variable">$acceptable</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token scope">Str<span class="token punctuation">::</span></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token variable">$acceptable</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token single-quoted-string string">'/json'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'+json'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

}

// 判断客服端是否使用 Pjax
public function pjax()
{
return $this->headers->get('X-PJAX') == true;
}

// 判断是否 Ajax 请求
public function ajax()
{
return $this->isXmlHttpRequest();
}

// 判断 Header 信息里 X-Requested-With 存在,并等于 XMLHttpRequest
public function isXmlHttpRequest()
{
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}

需要注意的是 ajax() 是判断 X-Requested-With 标头,而 wantsJson() 是判断 Accept 标头里是否存在 json,而 expectsJson() 是两种情况去除掉 Pjax 请求。

原文:https://learnku.com/laravel/wikis/25590

posted @ 2020-01-14 10:02  橱窗外的小孩  阅读(...)  评论(...编辑  收藏