0

I try to call data JSON from URL, and the source need authentication bearer I create in codeigniter framework this is my reference link

and here is my code,

$.ajax({
    url: "url",
    method: "GET",
    headers: {
        'Token': Bearer +'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTgwMTg1MjA0LCJleHAiOjE1ODAxODg4MDR9.dG13Tv62YHgdH9SD1HKPCc_JSAVgqrJb70UlR-bqy4E'
    },
    contentType: 'application/json',
    success: function(data) {

    },
    error: function (data, status, xhr) { 
        console.log(status); 
        console.log(xhr); 
    }
});

any help would be appreciated.

Prateik Darji
  • 2,297
  • 1
  • 13
  • 29
  • `Bearer` should be a the string "Bearer" - and usually you'd use `Authorization` as the header, but if the API requires `Token` then that's fine - though, reading the documentation you linked to, but obviously haven't read, the header should be `Authorization` not `Token` ... and as stated the `Bearer` is part of the header value, i.e. it's a literal string – Jaromanda X Jan 28 '20 at 04:43
  • i.e. your code would look like `Authentication: 'Bearer "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTgwMTg1MjA0LCJleHAiOjE1ODAxODg4MDR9.dG13Tv62YHgdH9SD1HKPCc_JSAVgqrJb70UlR-bqy4E"'` – Jaromanda X Jan 28 '20 at 04:49
  • headers: { 'Token' : Bearer "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTgwMTg5MjA5LCJleHAiOjE1ODAxOTI4MDl9.zSedo6KmsdKYnbIDxNBag79QQjPh_V4spFnqt4XS4ZQ"}, like this? – Aisyah alifia Jan 28 '20 at 05:41
  • i'am using Token not Authentication. thats right in documentation use authentication but i test with postman just works when i'm using Token @JaromandaX – Aisyah alifia Jan 28 '20 at 05:42
  • well, then the only problem is that you are trying to use a variable named `Bearer` instead of the string `Bearer` - and perhaps the token part needs to be in `"` - but since your actual requirements only partially resemble the code you're basing it on, then you'll need to tweak those minutiae – Jaromanda X Jan 28 '20 at 05:59
  • headers: { 'Token' : 'Bearer'+'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTgwMTg5MjA5LCJleHAiOjE1ODAxOTI4MDl9.zSedo6KmsdKYnbIDxNBag79QQjPh_V4spFnqt4XS4ZQ'}, like this? @JaromandaX – Aisyah alifia Jan 28 '20 at 06:03
  • It should be like `"Token": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTgwMTg1MjA0LCJleHAiOjE1ODAxODg4MDR9.dG13Tv62YHgdH9SD1HKPCc_JSAVgqrJb70UlR-bqy4E"` – Prateik Darji Jan 28 '20 at 06:05
  • Does this answer your question? [How to send a token with an AJAX request from jQuery](https://stackoverflow.com/questions/35861012/how-to-send-a-token-with-an-ajax-request-from-jquery) – Prateik Darji Jan 28 '20 at 06:13
  • @PrateikDarji nope bro – Aisyah alifia Jan 28 '20 at 08:10
  • is there any error in console after ajax call? – Prateik Darji Jan 28 '20 at 08:50
  • does the token need to be sent in quotes after the word "Bearer" ...only you know the server you're sending to, it's nothing like the example you linked to, so ... – Jaromanda X Jan 28 '20 at 10:54
  • @JaromandaX, so far I think now the problem is not in ajax but on configuration access control header API, because with the same ajax code i can get data from another url, do you have an idea? – Aisyah alifia Jan 29 '20 at 02:58
  • My gut says the server side is expecting something other than what you are sending – Jaromanda X Jan 29 '20 at 03:02

0 Answers0