skip to Main Content

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?

2

Answers


  1. You just need to add header property to the object

    $.ajax({
        url: 'http://localhost:8085/api/test',
        headers: {'token': 'test'}
    });
    
    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search