0

I am creating a simple jQuery powered application, which uses JWT via Flask on the back-end. I do not want to use a more feature rich client-side framework.

I can interface with the API using the token via $.ajax such as:

$.ajax({
    type: 'post',
    url: '/users',
    dataType: 'json',
    beforeSend: function (xhr) {
        xhr.setRequestHeader("Authorization", "Bearer XXX")
    }
});

However, I cannot interface with the 'simple' HTML pages as the GET request does not include the Bearer token.

If I attempt to use the same method as above, but using a GET type, the ajax can load the page, however the new content is not displayed in the browser (XMLHttpRequest).

Is there a way I can access and display GET data in the users browser using jQuery?

Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37
user1220022
  • 11,167
  • 19
  • 41
  • 57
  • Should work with GET. Possible something wrong with server-side code? Check answers for [this question](https://stackoverflow.com/a/19766468/1397220) to see if you did something wrong. (seems okay for me) – Brainfeeder May 07 '18 at 12:56
  • Thanks for the link, I have a similar snippet above. the GET request itself works fine (I can see in the network tab), but the browser page itself does not refresh/reload the content which is requested over ajax – user1220022 May 07 '18 at 12:58
  • I thought question was about `However, I cannot interface with the 'simple' HTML pages as the GET request does not include the Bearer token.` – Brainfeeder May 07 '18 at 13:01
  • Sorry if it was not clear. I can make GET/POST requests to the API. This works fine. But any direct browser requests do not work. As the bearer token is not included. For example, if you visit a link /users, the token will not be inlcuded in the request. – user1220022 May 07 '18 at 13:07
  • Then you'll have to alter the server-side script so that if the request is a simple page request, he responses with html and only for ajax calls it should check for the token. – Brainfeeder May 07 '18 at 13:11
  • 1
    Add done callback `$.ajax(...).done(function(data) {console.log(data);});` – Camille May 07 '18 at 13:52
  • If you have a tokken : `$.ajax({...,headers: {'X-Auth-Token' : token}});` – Camille May 08 '18 at 06:31

0 Answers0