0

I'm trying to get some data using JQuery and REST. I can get logged in and receive an auth token but I can't seem to get past "Error 400 BAD REQUEST" below is my code

var token;
var creds = {
  'username': '',
  'password': ''
}

function login(uid, pwd) {
  creds.username = uid;
  creds.password = pwd;

  $.ajax({
    type: 'POST',
    data: JSON.stringify(creds),
    url: 'http://localhost..../login',
    contentType: 'application/json; chartset=utf-8',
    dataType: 'json',
    async: false,
    success: function(data) {
      token = data.token;
    },
    error: function() {
      console.log("fail");
    });
}

function getData() {
  $.get('localhost..../node/1?token=' + token, function(data) {
    console.log(data);
  });
}

All of the code above works except for getData(). That produces an error 400 bad request. The URL that's being generated, when pasted into a browser, will infact return the expected results. I event attempted to use the same ajax request in login() for getData() but it produced the same error. All of the code above is what I'm using for the exception of the URLs in the ajax get get requests.

user3780616
  • 1,183
  • 1
  • 10
  • 23
  • Itching to close as dupe http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – Musa Feb 26 '15 at 23:44

1 Answers1

0

I had to add my token as a header

$.ajax({
  .....
  headers: {
    "token": ....
  },
  .....
});
user3780616
  • 1,183
  • 1
  • 10
  • 23