I want to have an input field that would suggest stock tickers as the user types in letters, like you'd see on Yahoo or Google Finance. I can't just use JSON unless I enable CORS, in which case it works. Here's code that I cobbled together from various questions I found on Stack Overflow (here and here) while researching the problem I'm facing.
$("input#stocklookup").autocomplete({
source: function(request, response){
$.ajax({
url: 'http://d.yimg.com/autoc.finance.yahoo.com/autoc?query='+request.term+'®ion=US&lang=en-US',
dataType: 'jsonp',
jsonpCallback: 'YAHOO.util.ScriptNodeDataSource.callbacks'
});
YAHOO = {
util: {
ScriptNodeDataSource: {
callbacks: function(data) {
console.log(data);
}
}
}
};
}
})
Searching for 'a', I will get a 400 error and the URL is listed as
http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=a®ion=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks&_=1462660878200
I think the extra numbers at the end are what may be causing the 400, but I don't know why it's being appended to the URL. If you navigate to the URL above, the results show properly in your browser.
I also tried using this block of code after seeing this page in the learn jQuery docs, but I also get a 400 error using this.
.$ajax({
url: 'http://d.yimg.com/autoc.finance.yahoo.com/autoc?region=US&lang=en-US',
jsonp: 'callback',
dataType: 'jsonp',
data: {
q: request.term,
format: 'json'
},
success: function(response){
console.log(response);
}
})
Any help to get me in the right direction would be appreciated. Thanks.
Update
$.ajax({
url: 'http://d.yimg.com/autoc.finance.yahoo.com/autoc?query='+request.term+'®ion=US&lang=en-US',
cache: true, //<--new
dataType: 'jsonp',
jsonpCallback: 'YAHOO.util.ScriptNodeDataSource.callbacks'
});
YAHOO = {
util: {
ScriptNodeDataSource: {
callbacks: function(data) {
console.log(data);
}
}
}
};
This works for some requests but not others, some still return a 400 status.