用于自动完成 jquery 插件的 JSONP Freebase

发布于 2024-11-15 09:16:59 字数 1268 浏览 3 评论 0原文

这来自 用 PHP 解析 JSON Freebase 结果 但由于可以使用 JSONP 在 Javascript 中做到这一点,我想知道如何做。

所以,我正在使用这个 jquery 自动完成插件:http://devthought。 com/wp-content/projects/jquery/textboxlist/Demo/

这是在输入上使用插件的代码:

$(function() {
    var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
            minLength: 2,
            queryRemote: true,
            remote: {url: 'autocomplete2.php'}
        }}});

我想解析 Freebase 的结果,例如 http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

并传递它按此顺序添加到插件:

guid,"name",null,"name<span>n:type name</span>"

因此,第一个结果将是:

0,"The Beatles",null,"The Beatles<span>Band</span>"

This comes from Parse JSON Freebase results in PHP
But since its possible to do it in Javascript using JSONP, I would liek to know how.

So, I'm using this jquery Autocomplete plugin: http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/

This is the code for using the plugin on an input:

$(function() {
    var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
            minLength: 2,
            queryRemote: true,
            remote: {url: 'autocomplete2.php'}
        }}});

I would like to parse the results from Freebase, e.g. http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

and pass it to the plugin in this order:

guid,"name",null,"name<span>n:type name</span>"

So, the first result would be:

0,"The Beatles",null,"The Beatles<span>Band</span>"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-11-22 09:17:00
<input id="form_topick_tags" />

<!-- Adjust the script tag locations per your set-up -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="GrowingInput.js" type="text/javascript" charset="utf-8"></script>         
<script src="../Source/TextboxList.js" type="text/javascript" charset="utf-8"></script>     
<script src="../Source/TextboxList.Autocomplete.js" type="text/javascript" charset="utf-8"></script> 
<!-- required for TextboxList.Autocomplete if method set to 'binary' --> 
<script src="../Source/TextboxList.Autocomplete.Binary.js" type="text/javascript" charset="utf-8"></script>     


<script type="text/javascript">
var search = 'beatles',
    myJSONArray = [];

$(function() {
    var t = new $.TextboxList('#form_topick_tags', {
        unique: true, 
        plugins: {
            autocomplete: {
                minLength: 2
            }
        }
    });

    $.ajax({
        dataType:'JSONP',
        success: function (obj) {
            for (var i=0, orl=obj.result.length; i < orl; i++) {
                var o = obj.result[i];
                myJSONArray.push([o.guid, o.name, o.name+'<span>'+o['n:type'].name+'</span>']);
            }

            // For testing:

            // alert(myJSONArray);
            // You can just use myJSONArray, but if you need JSON, see http://json.org for a JSON converter; in modern browsers, JSON is supported by default
            //alert(JSON.stringify(myJSONArray)); 

            t.plugins['autocomplete'].setValues(myJSONArray);
        }, 
        url: 
        'http://www.freebase.com/private/suggest?type_strict=any&category=object&all_types=false&start=0&limit=10&prefix='+encodeURIComponent(search)
    });
});
</script>
<input id="form_topick_tags" />

<!-- Adjust the script tag locations per your set-up -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="GrowingInput.js" type="text/javascript" charset="utf-8"></script>         
<script src="../Source/TextboxList.js" type="text/javascript" charset="utf-8"></script>     
<script src="../Source/TextboxList.Autocomplete.js" type="text/javascript" charset="utf-8"></script> 
<!-- required for TextboxList.Autocomplete if method set to 'binary' --> 
<script src="../Source/TextboxList.Autocomplete.Binary.js" type="text/javascript" charset="utf-8"></script>     


<script type="text/javascript">
var search = 'beatles',
    myJSONArray = [];

$(function() {
    var t = new $.TextboxList('#form_topick_tags', {
        unique: true, 
        plugins: {
            autocomplete: {
                minLength: 2
            }
        }
    });

    $.ajax({
        dataType:'JSONP',
        success: function (obj) {
            for (var i=0, orl=obj.result.length; i < orl; i++) {
                var o = obj.result[i];
                myJSONArray.push([o.guid, o.name, o.name+'<span>'+o['n:type'].name+'</span>']);
            }

            // For testing:

            // alert(myJSONArray);
            // You can just use myJSONArray, but if you need JSON, see http://json.org for a JSON converter; in modern browsers, JSON is supported by default
            //alert(JSON.stringify(myJSONArray)); 

            t.plugins['autocomplete'].setValues(myJSONArray);
        }, 
        url: 
        'http://www.freebase.com/private/suggest?type_strict=any&category=object&all_types=false&start=0&limit=10&prefix='+encodeURIComponent(search)
    });
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文