开发者

Undefined Value Returned for Text Input and Display YQL Query Results

I am trying to display content from a YQL Query. Howe开发者_如何学Cver, the value I am entering into my text field is returning a value of undefined when I submit it, thus causing the YQL query to fail.

Along with this issue, I'm not sure how to select just the titles from the data argument in the callback function for the getJSON function in this portion of my code:

//Return the JSON results of the YQL query
$.getJSON(restQuery, function(data) {
    //Display the returned results in the searchResults div
    $("#searchResults").html(data);
});

Here is the JSFiddle of my code: http://jsfiddle.net/JAS4H/30/

Thanks in advance for any help!


I've cleaned up your code a bit.

$(function() {

    function search(term) {
        var query = 'SELECT title FROM search.web WHERE query="' + term + '"',
            url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query) + '&format=json&diagnostics=true&callback=cbfunc';

        $.get(url, function(data) {
            $('#searchResults').html(data);
        });
    }

    $('#search').live('submit', function() {
        search($('#searchInput').val());
        return false;
    });

});

Demo: http://jsfiddle.net/JAS4H/33/

As you can see, the response data is not JSON - that's why $.getJSON() does not work. For instance, if you search for "cars", you will get this response:

cbfunc({"query":{"count":10,"created":"2011-02-21T20:40:16Z","lang":"en-US","diagnostics":{"publiclyCallable":"true","url":{"execution-time":"779","content":"http://boss.yahooapis.com/ysearch/web/v1/cars?format=xml&start=0&count=10"},"user-time":"781","service-time":"779","build-version":"11323"},"results":{"result":[{"title":"New & Used Cars for Sale, Auto Dealers, Car Reviews and Car ..."},{"title":"Cars (Movie)"},{"title":"AutoTrader"},{"title":"New Cars, Used Cars, Blue Book Values & Car Prices - Kelley ..."},{"title":"Automobile - Wikipedia, the free encyclopedia"},{"title":"Car Allowance Rebate System (CARS)"},{"title":"Used Cars - Used Car Prices, Used Car Values & Reviews ..."},{"title":"Edmunds.com"},{"title":"Cars For Sale, Used Cars For Sale, New Cars For Sale ..."},{"title":"Research New Cars & Used Cars : Automobile Prices, Specs ..."}]}}});

This is JSONP I believe ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜