Summary of the methods of setting up and obtaining custom header information in js ajax

Keywords: PHP encoding IIS

Catalog

1. Set the custom header in js ajax

1.1 method 1:

$.ajax({
    type: "POST",
    url: "Handler1.ashx",
    contentType: "application/x-www-form-urlencoded",
    beforeSend: function (request) {
        request.setRequestHeader("token1", "Chenxizhang");
    },
    success: function (data) {
        //your code
    }
});

1.2 method 2:

$.ajax({
    headers: {
        "testheader": "test"
    },
    type: "POST",
    url: "Handler1.ashx",
    contentType: "application/x-www-form-urlencoded",
    success: function (data) {
        //your code
    }
});

2. js ajax obtains the header information of the response returned by the request

When the ajax request is completed, the xhr (XMLHTTPRequest) object will be returned, which contains the returned header information. You can get the header information through getResponseHeader(key) and getAllResponseHeaders();

$.ajax({
    type: "POST",
    url: "Handler1.ashx",
    contentType: "application/x-www-form-urlencoded",
    success: function (data) {
        //your code
    },
    complete: function (xhr, data) {
        /* 
            Get related Http Response header
            getResponseHeader(key): Get the specified header information
            getAllResponseHeaders(): Get all the header information available by default
        */
        var date=xhr.getResponseHeader('Date');// Server time
        
        //Get the header information customized by the server
        var stoken = xhr.getResponseHeader('servertoken');
        
        var list = xhr.getAllResponseHeaders();
        console.log(list);
        /*
        date: Fri, 12 Jul 2019 12:41:00 GMT
        content-encoding: gzip
        server: Microsoft-IIS/10.0
        x-aspnet-version: 4.0.30319
        x-powered-by: ASP.NET
        vary: Accept-Encoding
        content-type: text/plain; charset=utf-8
        servertoken: test1
        cache-control: private
        content-length: 129
        */
        
    }
});

3. Get customized header information when js ajax cross domain requests

When JS AJAX cross domain requests, it is not allowed to set the user-defined header information, but the user-defined header information of the server can be obtained from the response, provided that the server has set access control expose headers;
The following is an example of ASP.NET server:

public void ProcessRequest(HttpContext context)
{
    context.Response.AddHeader("Access-Control-Allow-Origin", "*");
    context.Response.AddHeader("Access-Control-Allow-Headers", "*");
    context.Response.AddHeader("Access-Control-Allow-Methods", "*");
    //Custom header information
    context.Response.AddHeader("servertoken", "test");
    context.Response.AddHeader("Access-Control-Expose-Headers", "servertoken");
    context.Response.ContentType = "text/plain";
    context.Response.Write("Hello World");
}

-------------------

Posted by evan18h on Tue, 29 Oct 2019 13:22:01 -0700