简体   繁体   English

在ajax中设置请求头

[英]Setting request header in ajax

I have trouble with a piece of ajax code. 我遇到了一段Ajax代码的麻烦。 This is the error message I get from mod security: 这是我从mod安全获得的错误消息:

/ajaxController.php?mod=catalog&op=ajaxSeenProducts HTTP/1.1 /ajaxController.php?mod=catalog&op=ajaxSeenProducts HTTP / 1.1

Access denied with code 400 (phase 2). 使用代码400拒绝访问(阶段2)。 Operator EQ matched 0 at REQUEST_HEADERS. 运算符EQ在REQUEST_HEADERS处匹配0。 [file "/usr/local/apache/conf/modsec2.user.conf"] [line "12"] [id "960012"] [msg "POST request must have a Content-Length header"] [severity "WARNING"] [tag "PROTOCOL_VIOLATION/EVASION"] [文件“ /usr/local/apache/conf/modsec2.user.conf”] [行“ 12”] [id“ 960012”] [msg“ POST请求必须具有Content-Length标头”] [严重性“警告” ] [标签“ PROTOCOL_VIOLATION / EVASION”]

There is a module in the web shop I am working on, which shows the last viewed products, and the jquery code I have is this: 我正在处理的网上商店中有一个模块,其中显示了最近查看的产品,而我拥有的jquery代码是这样的:

$('#seen_products_box').ready(function() {
   $.ajax({  
      type: "POST",
      url: "{HOME}ajaxController.php?mod=catalog&op=ajaxSeenProducts",
      success: function(seenProducts) {
          $('#seen_products_box').empty();
          $('#seen_products_box').append(seenProducts);
      }
   });
});

While searching for the solution, I have found in various places that I need to set the 在寻找解决方案时,我发现需要在各个地方设置

Content-length 内容长度

parameter, but all I have found is in this format: 参数,但是我发现的所有格式都是:

xmlHttp.setRequestHeader("Content-length", params.length);

Is there any way to set the request header in the code format I have provided above? 有什么方法可以按照我上面提供的代码格式设置请求标头? I have already tried something like: 我已经尝试过类似的东西:

headers: { "Content-Length": params.length } 标头:{“ Content-Length”:params.length}

but with no result. 但没有结果。 Any help would be appreciated. 任何帮助,将不胜感激。

use code like: 使用如下代码:

                var url = '{HOME}ajaxController.php?';
                var data = "mod=catalog&op=ajaxSeenProducts";
                $.ajax({
                    url: url,
                    data: data,
                    dataType: "html",
                    type:'post',
                    success:function(seenProducts){
                        $('#seen_products_box').empty();
                        $('#seen_products_box').append(seenProducts);
                    },
                    error:function(response){

                    }
                });

you can use the beforeSend function to set the headers 您可以使用beforeSend函数设置headers

$('#seen_products_box').ready(function() {
$.ajax({  
  type: "POST",
beforeSend: function (request)
{
            request.setRequestHeader("Authority", authorizationToken);
},
  url: "{HOME}ajaxController.php?mod=catalog&op=ajaxSeenProducts",
  success: function(seenProducts) {
      $('#seen_products_box').empty();
      $('#seen_products_box').append(seenProducts);
   }
 });
});

or other way you can do it like 或其他方式可以做到

$('#seen_products_box').ready(function() {
$.ajax({  
  type: "POST",
 headers: {"X-Test-Header": "test-value"},
  url: "{HOME}ajaxController.php?mod=catalog&op=ajaxSeenProducts",
  success: function(seenProducts) {
      $('#seen_products_box').empty();
      $('#seen_products_box').append(seenProducts);
   }
 });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM