Home > Web Front-end > JS Tutorial > Detailed explanation of jQuery setting request information in header

Detailed explanation of jQuery setting request information in header

小云云
Release: 2018-01-23 13:51:55
Original
1991 people have browsed it

This article mainly introduces the method of jQuery setting request information in the header. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone.

jquery is a class library of js. js itself cannot operate the header, because js starts to be executed when the browser loads the page. The header needs to be performed on the server side.

If it is ajax, you can set the header

$.ajax({
  url: "",
  data: {},
  type: "GET",
  beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},//这里设置header
  success: function() {}
});
Copy after login

That is, the setRequestHeader function

How to set a special RequestHeader in the ajax request

Now the ajax application has It's quite extensive and there are many good ajax frameworks available. Ajax is an asynchronous request, and it is mainly a client-side script behavior. So, how to add some special header information to the request before the request?

The following is a simple example. I wrote it in jQuery. In its ajax function, there is a beforeSend method. This method accepts a parameter, which represents the XMLHttpRequest object that initiates the asynchronous request. We can use this object The setRequestHeader method achieves our purpose

Why setRequestHeader?

For example, in a real-time communication system, every time you get a message or send a message, you need to determine whether the user is still connected. By setting "accessToken", you can achieve normal communication;

beforeSend: function(request) {
   request.setRequestHeader("accessToken", accessToken);
},
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
  <script src="jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
  <script type="text/javascript" language="javascript">
    $(function() {
      $("#test").click(function() {
        $.ajax({
          type: "GET",
          url: "default.aspx",
          beforeSend: function(request) {
            request.setRequestHeader("Test", "Chenxizhang");
          },
          success: function(result) {
            alert(result);
          }
        });
      });
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <p id="v">
  </p>
  <input type="button" value="测试" id="test" />
  </form>
</body>
</html>
Copy after login

jQuery.ajax() How to set the Accept content in Headers

It’s actually very simple. First, if it is a common type, please set the dataType attribute directly

$.ajax({
  dataType: "json",
  type: "get",
  success: function (data) {
  }
});
Copy after login

Set dataType After that, you will go to the accepts attribute (this attribute will preset some common types) and directly add the corresponding type to Accept.

)%X02M](8[BKGW21{EY{0GD

If you want to customize the Accept content that is not in jQuery, you can manually set the accepts attribute, use key-value pair storage, and then Set the dataType attribute to the key you just customized.

$.ajax({
  accepts: {
    xxx: "application/xxx"
  },
  dataType: "xxx",
  type: "get",
  success: function (data) {
  }
});
Copy after login

Of course, you can also directly set the headers attribute and directly write the content of Accept.

$.ajax({
  headers: {
    Accept: "application/json; charset=utf-8"
  },
  type: "get",
  success: function (data) {
  }
});
Copy after login

Related recommendations:

How to remove the referer information in the header

php Get cookies from the Header_PHP tutorial

php Send custom data through the header Detailed explanation

The above is the detailed content of Detailed explanation of jQuery setting request information in header. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template