开发者

Using PHP instead of JSON in jQuery instant search script

I have a Google Instant style search script written in jQuery which pulls results from the JSON BingAPI. How can I make my script pull content from a PHP script rather than the BingAPI?

Here is my code:

$(document).ready(function(){
    $("#search").keyup(function(){
        var search=$(this).val();
        var keyword=encodeURIComponent(search);
        var yt_url='http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=?&Appid=642636B8B26344A69F5FA5C22A629A163752DC6B&query='+keyword+'&sources=web';
        window.location.hash=keyword;

        $.ajax({
            type:"GET",
            url:yt_url,
            dataType:"jsonp",
            success:function(response){
                $("#result").html('');
                if(response.SearchResponse.Web.Results.length){
                    $.each(response.SearchResponse.Web.Resu开发者_JS百科lts, function(i,data){
                        var title=data.Title;
                        var dis=data.Description;
                        var url=data.Url;
                        var final="<div class='webresult'><div class='title'><a href='"+url+"'>"+title+"</a></div><div class='desc'>"+dis+"</div><div class='url'>"+url+"</div></div>";
                        $("#result").append(final);
                    });
                }
            }
        });
    });
});


Replace this:

var yt_url='http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=?&Appid=642636B8B26344A69F5FA5C22A629A163752DC6B&query='+keyword+'&sources=web';

With your json suggest url

var yt_url='http://yourwebsite/json.php?query='+keyword;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜