Go to jquery How to set the request information in the header

Source: Internet
Author: User

jquery is the class library of JS, JS itself can not operate the header, because JS is in the browser load page process only to start execution of the header needs server-side operation if it is Ajax, can be set Header$.ajax ({    URL: "",    Data: {},    type: "GET",    beforesend:function (XHR) {xhr.setrequestheader (' x-test-header ', ' test-value ');},// This sets the header    Success:function () {}});

Which is the setRequestHeader function.

How to set a special Requestheader in an AJAX request

Ajax applications are now quite extensive and there are plenty of good Ajax frameworks to use. Ajax is an asynchronous request and is primarily a client-side scripting behavior. So how do you add some special header information to the request before the request?

Here is a simple example, I write in jquery, its Ajax function, there is a Beforesend method, this method takes a parameter, represents the XMLHttpRequest object that initiates the asynchronous request, We can use the object's setRequestHeader method to achieve our goal.

Why are you setrequestheader? For example, in a timely communication system, every time to get a message or send a message, you need to determine whether the user is still in the connection, by setting "Accesstoken", it can be to achieve normal communication;
Beforesend:function (Request) {     Request.setrequestheader ("Accesstoken", Accesstoken);},

 1 <%@ page language= "C #" autoeventwireup= "true" codefile= "Default2.aspx.cs" inherits= "Default2"%> 2 3 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 4 

Go to jquery How to set the request information in the header

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.