Trouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?

Shaun Reynolds's Avatar

Shaun Reynolds

11 Dec, 2013 12:35 AM

Here is a super-basic jQuery call to list my lighthouse projects. It doesn't work, returning a 302 redirect to http://xxxx.lighthouse.app/login What am I doing wrong here?

I can curl using the same url and api key and that works just fine, so I know the api key is valid...
curl -H 'X-LighthouseToken:xxxxxxx' http://xxxxx.lighthouseapp.com/projects.json

Thanks for reading!


LightHouse = {};
LightHouse.apiToken = 'xxxxxxxxxxxxxx';
LightHouse.baseURL = 'http://xxxxx.lighthouseapp.com/';

LightHouse.getProjects = function(callback) {
    var url = LightHouse.baseURL + 'projects.json';
    $.ajax({
        url: url,
        type: 'GET',
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Content-Type","application/json");
            xhr.setRequestHeader('X-LighthouseToken', LightHouse.apiToken);
        },
        success: function( data, textStatus, jqXHR ) {
            var returnVal = $.parseJSON(data);
            console.log(returnVal);
        }
    });
};
  1. Support Staff 1 Posted by Tiger Team on 11 Dec, 2013 12:38 AM

    Tiger Team's Avatar

    Is this code going to be public anywhere? Your token can be used for all sorts of evil.

  2. 2 Posted by Shaun Reynolds on 11 Dec, 2013 12:39 AM

    Shaun Reynolds's Avatar

    No, it won't be public. I'm just making some simple scripts to automate ticket tracking for some internal tools.

  3. Support Staff 3 Posted by Tiger Team on 11 Dec, 2013 12:42 AM

    Tiger Team's Avatar

    I see a bunch of requests sending OPTIONS requests instead of GET. Your ajax request isn't working...

    site:77075 user:nil [302 Found] OPTIONS /projects.json action= params={"method"=>:get, "paths"=>["projects.json"]} format=*/* ip=76.14.71.239 location=http://owngroup.lighthouseapp.com/login duration=5ms db=1ms

  4. 4 Posted by Shaun Reynolds on 11 Dec, 2013 01:02 AM

    Shaun Reynolds's Avatar

    I figured it out. jQuery was setting headers in such a way that it was triggering a cross-site scripting preflight request.

    setting a dataType of 'jsonp' fixed it...

    $.ajax({
        headers: { 'X-LighthouseToken' : LightHouse.apiToken },
        url: url,
        type: 'GET',
        dataType: "jsonp",
        success: function( data, textStatus, jqXHR ) {
            console.log('yippiee');
        }
    });
    
  5. 5 Posted by Shaun Reynolds on 11 Dec, 2013 01:03 AM

    Shaun Reynolds's Avatar

    Thanks for your very prompt responses :)

  6. Support Staff 6 Posted by Tiger Team on 11 Dec, 2013 01:14 AM

    Tiger Team's Avatar

    no worries good luck getting it all integrated!

Discussions are closed to public comments.
If you need help with Lighthouse please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac