2

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+'&region=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&region=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+'&region=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.

jcharch
  • 150
  • 16
  • The extra stuff is probably being appending onto the end by jQuery just to prevent browser caching. You can turn that off in jQuery,'s ajax call, but then caching of the JSONP request/response might mess you up. – jfriend00 May 08 '16 at 02:55
  • 1
    FYI, if you just enter this into your browser: `http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=a&region=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks&_=1462660878200`, you will see a JSONP response so I don't think your error has anything to do with the `&_=1462660` part of the URL. – jfriend00 May 08 '16 at 02:58
  • Thanks!! I put cache: true in and it worked! Caching isn't an issue, individual stock tickers don't change all that often. – jcharch May 08 '16 at 06:33
  • Uhhh, caching can be a huge issue for stock tickers. I don't know what your app is, but the last thing people want is day old stock info. – jfriend00 May 08 '16 at 06:47
  • Now, curiously enough, some letters work and some don't. http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=w&region=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks returns status of 200, http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=r&region=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks returns status of 400. Updating original question – jcharch May 08 '16 at 06:54
  • check out the json returned by yahoo, you might be getting an error message there – Velimir Tchatchevsky Jun 08 '16 at 10:18

2 Answers2

3

I fixed it!

Added this in to my HTML file.

<meta charset="UTF-8" name="referrer" content="no-referrer">
jcharch
  • 150
  • 16
0

Just a heads up - I have a couple of projects (Java and JavaScript) which call this API. They usually work but occasionally fail with a 400 without any change to the code - then work a few hours/days later, again, without changing the code. I think if there is some problem with the server it may return this rather than a correct error in the 500 range (server error - It's me, not you)

Errors in the 400 range should be a message from the server along the lines of "it's you, not me - fix your request before you send it again" but I don't think this is the case with this API.

For example - one of my requests which worked, didn't work, did work and then didn't is:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20=%20%22USDGBP%22&env=store://datatables.org/alltableswithkeys

in a browser I get the same 400 error but the following XML....

<error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-US">
<description>
No definition found for Table yahoo.finance.xchange
</description>
</error>

In short - it may be them not you!

user1276925
  • 269
  • 2
  • 5