<bdo id='LQIp6'></bdo><ul id='LQIp6'></ul>
  • <tfoot id='LQIp6'></tfoot>
    <legend id='LQIp6'><style id='LQIp6'><dir id='LQIp6'><q id='LQIp6'></q></dir></style></legend>

      1. <i id='LQIp6'><tr id='LQIp6'><dt id='LQIp6'><q id='LQIp6'><span id='LQIp6'><b id='LQIp6'><form id='LQIp6'><ins id='LQIp6'></ins><ul id='LQIp6'></ul><sub id='LQIp6'></sub></form><legend id='LQIp6'></legend><bdo id='LQIp6'><pre id='LQIp6'><center id='LQIp6'></center></pre></bdo></b><th id='LQIp6'></th></span></q></dt></tr></i><div id='LQIp6'><tfoot id='LQIp6'></tfoot><dl id='LQIp6'><fieldset id='LQIp6'></fieldset></dl></div>
      2. <small id='LQIp6'></small><noframes id='LQIp6'>

        如何判断一个请求是否是ajax请求?

        How to find out if a request is an ajax request?(如何判断一个请求是否是ajax请求?)
          <tbody id='2qydR'></tbody>
        <tfoot id='2qydR'></tfoot>
        <legend id='2qydR'><style id='2qydR'><dir id='2qydR'><q id='2qydR'></q></dir></style></legend>
      3. <small id='2qydR'></small><noframes id='2qydR'>

        <i id='2qydR'><tr id='2qydR'><dt id='2qydR'><q id='2qydR'><span id='2qydR'><b id='2qydR'><form id='2qydR'><ins id='2qydR'></ins><ul id='2qydR'></ul><sub id='2qydR'></sub></form><legend id='2qydR'></legend><bdo id='2qydR'><pre id='2qydR'><center id='2qydR'></center></pre></bdo></b><th id='2qydR'></th></span></q></dt></tr></i><div id='2qydR'><tfoot id='2qydR'></tfoot><dl id='2qydR'><fieldset id='2qydR'></fieldset></dl></div>

                • <bdo id='2qydR'></bdo><ul id='2qydR'></ul>
                  本文介绍了如何判断一个请求是否是ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试确定对 PHP 文件的请求是否由 ajax 发送.

                  我在谷歌上搜索并阅读了一大堆建议使用以下方法的文章:

                  I googled it and read a whole a bunch of articles that suggest following method:

                  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {     
                      echo 'This is an ajax request!';   
                      exit;
                  }
                  echo 'This is not an ajax request!';
                  

                  但是我的服务器没有这个变量:未定义索引:HTTP_X_REQUESTED_WITH ...

                  But my server doesn't have this variable: Undefined index: HTTP_X_REQUESTED_WITH ...

                  这就是我发出ajax请求的方式:

                  Thats how I make the ajax request:

                  $.ajax({
                      url: 'http://URL/test.php',
                      complete: function(res) {
                          console.log(res.responseText);
                      }
                  });
                  

                  我是从不同的 url 进行调用,所以我设置了 header('Access-Control-Allow-Origin: *');

                  I'm making that call from a different url, so I've set header('Access-Control-Allow-Origin: *');

                  不过,我发现了 $_SERVER 的一个不同之处:

                  I have discovered one difference in $_SERVER though:

                  Ajax 请求:$_SERVER[HTTP_ACCEPT] =>*/*

                  无 Ajax 请求:$_SERVER[HTTP_ACCEPT] =>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

                  所以我的问题是,有没有办法让我将 HTTP_X_REQUESTED_WITH 放入 $_SERVER 中?如果不是,是否有一种正确的方法可以通过使用 $_SERVER[HTTP_ACCEPT]

                  So my question is, is there a way for me to get HTTP_X_REQUESTED_WITH into $_SERVER? And if not, is there a proper way to find out if the request is AJAX by using the $_SERVER[HTTP_ACCEPT]

                  推荐答案

                  没有 100% 的方法来检测请求是否是通过 ajax 发出的.即使有人发送带有X-Requested-With: XMLHttpRequest"的标头,您也不应该依赖它.

                  There's no 100% way to detect if the request was made via ajax. Even if someone sends header with "X-Requested-With: XMLHttpRequest" you shouldn't rely on it.

                  这篇关于如何判断一个请求是否是ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Appending GET parameters to URL from lt;formgt; action(将 GET 参数附加到来自 lt;formgt; 的 URL行动)
                  Forcing quot;Save Asquot; dialog via jQuery GET(强制“另存为通过 jQuery GET 对话框)
                  PHP - get certain word from string(PHP - 从字符串中获取某个单词)
                  How to debug a get request in php using curl(如何使用 curl 在 php 中调试 get 请求)
                  get a # from a url in php(从 php 中的 url 获取 #)
                  PHP - include() file not working when variables are put in url?(PHP - 将变量放入 url 时,include() 文件不起作用?)
                    <bdo id='DSVQu'></bdo><ul id='DSVQu'></ul>

                        <tbody id='DSVQu'></tbody>

                      <small id='DSVQu'></small><noframes id='DSVQu'>

                          <tfoot id='DSVQu'></tfoot>
                          1. <legend id='DSVQu'><style id='DSVQu'><dir id='DSVQu'><q id='DSVQu'></q></dir></style></legend>
                            <i id='DSVQu'><tr id='DSVQu'><dt id='DSVQu'><q id='DSVQu'><span id='DSVQu'><b id='DSVQu'><form id='DSVQu'><ins id='DSVQu'></ins><ul id='DSVQu'></ul><sub id='DSVQu'></sub></form><legend id='DSVQu'></legend><bdo id='DSVQu'><pre id='DSVQu'><center id='DSVQu'></center></pre></bdo></b><th id='DSVQu'></th></span></q></dt></tr></i><div id='DSVQu'><tfoot id='DSVQu'></tfoot><dl id='DSVQu'><fieldset id='DSVQu'></fieldset></dl></div>