<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请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <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>