XMLHttpRequest.setRequestHeader() - Web APIs | MDN

archived 7 Apr 2017 09:05:48 UTC
Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: https://goo.gl/forms/Ync2VuTWwAkQFvJx2
Your Search Results

    XMLHttpRequest.setRequestHeader()

    The XMLHttpRequest.setRequestHeader() method sets the value of an HTTP request header. You must call setRequestHeader()after open(), but before send(). If this method is called several times with the same header, the values are merged into one single request header.
    If no Accept header has been set using this, an Accept header with the */* is sent with the request when send() is called.
    For security reasons, some headers can only be controlled by the user agent. These headers include the forbidden header names  and forbidden response header names.
    For your custom fields, you may encounter "not allowed by Access-Control-Allow-Headers in preflight response" exception when you send request to cross domain. In this situation, you need set "Access-Control-Allow-Headers" in your response header at server side.

    SyntaxEdit

    myReq.setRequestHeader(header, value);
    

    Parameters

    header
    The name of the header whose value is to be set.
    value
    The value to set as the body of the header.

    SpecificationsEdit

    Specification Status Comment
    XMLHttpRequest
    The definition of 'setRequestHeader()' in that specification.
    LSLiving Standard WHATWG living standard

    Browser compatibilityEdit

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
    Basic support 1 ? 5[1]
    7
    (Yes) 1.2
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support ? 1.0 (Yes) ? ? ?
    [1] This feature was implemented via ActiveXObject(). Internet Explorer implements the standard XMLHttpRequest since version 7.

    See alsoEdit

    Document Tags and Contributors

     Contributors to this page: KeZhang, teoli, MashKao, jsx
     Last updated by: KeZhang,

    Thanks! Please check your inbox to confirm your subscription.

    If you haven’t previously confirmed a subscription to a Mozilla-related newsletter you may have to do so. Please check your inbox or your spam filter for an email from us.
    0%
    10%
    20%
    30%
    40%
    50%
    60%
    70%
    80%
    90%
    100%