tag:help.lighthouseapp.com,2008-09-20:/discussions/api-developers/1727-trouble-getting-started-would-you-kindly-tell-me-what-is-wrong-with-this-jquery-ajax-requestLighthouse: Discussion 2013-12-11T01:14:50Ztag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T00:35:12Z2013-12-11T00:38:43ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>Here is a super-basic jQuery call to list my lighthouse
projects. It doesn't work, returning a 302 redirect to <a href=
"http://xxxx.lighthouse.app/login">http://xxxx.lighthouse.app/login</a>
What am I doing wrong here?</p>
<p>I can curl using the same url and api key and that works just
fine, so I know the api key is valid...<br>
curl -H 'X-LighthouseToken:xxxxxxx' <a href=
"http://xxxxx.lighthouseapp.com/projects.json">http://xxxxx.lighthouseapp.com/projects.json</a></p>
<p>Thanks for reading!</p>
<hr>
<pre>
<code>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);
}
});
};</code>
</pre></div>Shaun Reynoldstag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T00:38:09Z2013-12-11T00:38:09ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>Is this code going to be public anywhere? Your token can be used
for all sorts of evil.</p></div>Tiger Teamtag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T00:39:52Z2013-12-11T00:39:52ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>No, it won't be public. I'm just making some simple scripts to
automate ticket tracking for some internal tools.</p></div>Shaun Reynoldstag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T00:42:27Z2013-12-11T00:42:27ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>I see a bunch of requests sending OPTIONS requests instead of
GET. Your ajax request isn't working...</p>
<p>site:77075 user:nil [302 Found] OPTIONS /projects.json action=
params={"method"=>:get, "paths"=>["projects.json"]}
format=*/* ip=76.14.71.239 location=<a href=
"http://owngroup.lighthouseapp.com/login">http://owngroup.lighthouseapp.com/login</a>
duration=5ms db=1ms</p></div>Tiger Teamtag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T01:02:46Z2013-12-11T01:02:46ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>I figured it out. jQuery was setting headers in such a way that
it was triggering a cross-site scripting preflight request.</p>
<p>setting a dataType of 'jsonp' fixed it...</p>
<pre>
<code>$.ajax({
headers: { 'X-LighthouseToken' : LightHouse.apiToken },
url: url,
type: 'GET',
dataType: "jsonp",
success: function( data, textStatus, jqXHR ) {
console.log('yippiee');
}
});</code>
</pre></div>Shaun Reynoldstag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T01:03:18Z2013-12-11T01:03:18ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>Thanks for your very prompt responses :)</p></div>Shaun Reynoldstag:help.lighthouseapp.com,2008-09-20:Comment/304829682013-12-11T01:14:49Z2013-12-11T01:14:49ZTrouble getting started: would you kindly tell me what is wrong with this jQuery ajax request?<div><p>no worries good luck getting it all integrated!</p></div>Tiger Team