-1

I have a problem with the ajax GET function. I can not send the header from my GET function. This is my ajax code:

 <script type="text/javascript">
  function login() {

  $.ajax({
            url: 'http://localhost:8085/api/test',
            beforeSend: function (xhr) {
                  xhr.setRequestHeader('token', 'test');
              },
            dataType: 'JSONP',
            jsonpCallback: 'callbackFnc',
            type: 'GET',
            async: false,
            crossDomain: true,
            success: function () { },
            failure: function () { },
        });
  }
</script>

Does anyone know where is the problem?

Rob
  • 14,746
  • 28
  • 47
  • 65
SIn san sun
  • 573
  • 4
  • 13
  • 31
  • Does this answer your question? [How can I add a custom HTTP header to ajax request with js or jQuery?](https://stackoverflow.com/questions/7686827/how-can-i-add-a-custom-http-header-to-ajax-request-with-js-or-jquery) – Ivar Jan 15 '20 at 12:18
  • Please note that `async: false` is deprecated and shouldn't be used. – Ivar Jan 15 '20 at 12:19

2 Answers2

0

You just need to add header property to the object

$.ajax({
    url: 'http://localhost:8085/api/test',
    headers: {'token': 'test'}
});
Archie
  • 901
  • 4
  • 11
0

You can set the headers:

<script type="text/javascript">
  function login() {

  $.ajax({
            url: 'http://localhost:8085/api/test',
            headers: {'token': 'test'}
            dataType: 'JSONP',
            jsonpCallback: 'callbackFnc',
            type: 'GET',
            crossDomain: true,
            success: function () { },
            failure: function () { },
        });
  }
</script>
Sezer
  • 35
  • 1
  • 7