2

I am trying to send an API key with headers but my header is not showing in my request or I'm unable to send the header request.

$(document).ready(function () {
    $.ajax({
        url: "http://xx.xx.xx.xx:xx/api/values",
        type: "Get",
        dataType: "json",
        Headers: { "HeaderName": "MYKey" },
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                var str = "<div>" + data[i].ID + ":" + data[i].APIKey + "</div>";
              //  $("#bookTemplate").append(str);
            }
        },
        error: function (msg) { alert(msg); }
    });
});

I have also tried using:

 beforeSend: function(xhr)   {xhr.setRequestHeader('Header Name', 'Mykey');},
Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
DineshChauhan
  • 121
  • 1
  • 4
  • 17
  • possible duplicate of [How can I add a custom HTTP header to ajax request with js or jQuery?](http://stackoverflow.com/questions/7686827/how-can-i-add-a-custom-http-header-to-ajax-request-with-js-or-jquery) – Shiva Mar 07 '15 at 08:23

1 Answers1

4

use lowercase for headers

$(document).ready(function () {
        $.ajax({
            url: "http://xx.xx.xx.xx:xx/api/values",
            type: "GET",
            dataType: "json",
            headers: { "HeaderName": "MYKey" }
        });
    });
marsh
  • 1,431
  • 1
  • 13
  • 19