Question

Here's my code in a javascript function in a HTML file

var url = 'http://query.yahooapis.com/v1/public/yql';
        var startDate = '2012-01-01';
        var endDate = '2012-01-08';
        var jsonData = encodeURIComponent('select * from yahoo.finance.historicaldata where symbol in ("YHOO","AAPL","GOOG","MSFT") and startDate = "' + startDate + '" and endDate = "' + endDate + '"');
        $.getJSON(url, 'q=' + data + "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json", function(){alert("done!");}); 

When i open the file in the browser, my other functions work except the above as it produces:

GET http://query.yahooapis.com/v1/public/yql?q=[object%20Object]&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json 400 **(Bad Request)** jquery-1.8.3.min.js:2
send jquery-1.8.3.min.js:2
v.extend.ajax jquery-1.8.3.min.js:2
v.(anonymous function) jquery-1.8.3.min.js:2
v.extend.getJSON jquery-1.8.3.min.js:2
drawChart
Was it helpful?

Solution

Should this:

$.getJSON(url, 'q=' + data + "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json", function(){alert("done!");}); 

be this?

$.getJSON(url, 'q=' + jsonData + "&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json", function(){alert("done!");}); 

OTHER TIPS

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.

In short - it may be them not you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top