War diese Seite hilfreich?
Ihr Feedback ist uns wichtig. Teilen Sie uns Ihre Meinung mit.
Weiteres Feedback?
1500 verbleibende Zeichen
MSDN Library
EN
Dieser Inhalt ist in Ihrer Sprache leider nicht verfügbar. Im Folgenden finden Sie die englische Version.

setRequestHeader method

Adds custom HTTP headers to the request.

Syntax

XMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);

Parameters

bstrHeader [in]

Type: String

String that specifies the header name.

bstrValue [in]

Type: String

String that specifies the header value.

Return value

This method does not return a value.

Standards information

Remarks

Refer to RFC2616, Section 14: Header Field Definitions for a general list of standard headers. The server is ultimately responsible for honoring the headers of the request. By far the most common request header is Content-Type, which is required by some XML Web services.

setRequestHeader was introduced in Windows Internet Explorer 7.

Examples

The following script sets the HTTP Content-Type header to 'text/xml' before sending the request body:


var oReq = new XMLHttpRequest();
oReq.open("POST", sURL, false);
oReq.setRequestHeader("Content-Type", "text/xml");
oReq.send(sRequestBody);

This example lets you load and display sections of a file. The example should be run on a server (localhost is fine).


<!DOCTYPE html>
<html >
  <head>
    <title>setRequestHeader example</title>

  </head>
  <body>
    <div>Enter a file, start and end point of a range. </div><br />
  
    <label>Start Range: <input id="startRange" placeholder="Type a place to start" /></label><br />
    <label>End Range: <input id="endRange" placeholder="Type a place to end"/></label><br />
    <label>File: <input id="filename" placeholder="Type a text file" /></label><br />
    <button id="getFile">Get file</button><br />
    <div id="output" style="display:block; overflow:auto; max-width:800px"></div>
        
    <!-- Put script below HTML to ensure elements are loaded --> 
    <script>
      //  Get data from the HTML elements 
      document.getElementById("getFile").addEventListener("click", function () {
        var url = document.getElementById("filename").value.toString();
        var range = document.getElementById("startRange").value.toString();
        range += "-" + document.getElementById("endRange").value.toString();
        getData(url, range);
      }, false);

          // Get data from file  
      function getData(url, range) {
        if (url !== "") {
          var xhr = new XMLHttpRequest(); // Set up xhr request
          xhr.open("GET", url, true);   // Open the request
          xhr.responseType = "text";   // Set the type of response expected
          // If there's a range set, create a request header to limit to that range
          if (range !== undefined && range !== null && range.length > 0) {
            xhr.setRequestHeader("Range", "bytes=" + range);
          }
          xhr.send();

          //  Asynchronously wait for the data to return
          xhr.onreadystatechange = function () {
            if (xhr.readyState == xhr.DONE) {
              var tempoutput = xhr.response;
              document.getElementById("output").innerHTML = tempoutput;
            }
          }
          //  Report errors if they happen
          xhr.addEventListener("error", function (e) {
            console("Error: " + e + " Could not load url.");
          }, false);
        }
      }
    </script>

  </body>
</html>


See also

XMLHttpRequest
getResponseHeader

 

 

Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?
Anzeigen:
© 2015 Microsoft