Ajax request with authorization

1.2k views Asked by At

I try to make ajax request with autorization header:

enter image description here

but get: GET http://agroagro.com/agroMobile/v1/tasks 400 (Bad Request)

my ajax code:

$(function() { 

if (localStorage.getItem('apiKey') == '') {
window.location.replace("login.html");  
}
    else {  

var urlAjax = "http://agroagro.com/agroMobile/v1/tasks";

$.ajax({
type:'GET',
url: urlAjax,
contentType: "application/x-www-form-urlencoded",
headers: {
    "Authorization": localStorage.getItem('apiKey')
  },
beforeSend: function(jqXHR) {
    console.log(localStorage.getItem('apiKey'));
    jqXHR.setRequestHeader("Authorization", localStorage.getItem('apiKey'));
},

  success: function(data) { 

    console.log('Lets do something');  


  },
error: function(data) {
    //window.location.replace("login.html");
    console.log(data); 
    console.log(console.log(localStorage.getItem('apiKey')));
  $('#error').append('<p>Please enter the correct information</p>');
    },
dataType: 'json',

});

    }

});

So why I get this error? As you can see I send right autorization api key and everything is fine but just dont work...

1

There are 1 answers

14
Zack Newsham On BEST ANSWER

By manually accessing the URL I can see you are missing an API key. You should be able to see the content of your request in the preview/response tabs of the console. That will likely give you a lot of information.

Have you read the API docs for the service you are connecting to?