【问题标题】:Using devbridge autocomplete and wunderground autocomplete API使用 devbridge 自动完成和 wunderground 自动完成 API
【发布时间】:2014-05-31 17:06:33
【问题描述】:

我正在尝试使用 devbridge jquery 自动完成库来提取 wunderground.com 的自动完成 API,但我一直受阻。无论我是否将 cb 附加到 serviceUrl,它都无法解析返回的 json。响应以“{RESULTS: [{ 我要使用的数组数据 }]}”为前缀。

当我使用文档中提供的自动完成代码时,它显示“Uncaught SyntaxError: Unexpected token :”

当我将 &cb=myresults 应用到 serviceUrl 时,我得到“未捕获的 ReferenceError: myresults is not defined”

我的代码是:

var options, a;
$(function(){
    options = {
        serviceUrl:'http://autocomplete.wunderground.com/aq?c=US&format=jsonp&cb=myresults',
        minChars: 7,
        dataType : "jsonp",
        transformResult: function(response) {
            response = JSON.parse(response);
                return {
                    suggestions: $.map(response.myData, function(dataItem) {
                        return { value: dataItem.name, data: dataItem.zmw };
                })
            };
        }
    };
    a = $('#autoLocation').autocomplete(options);
});

神奇的 API 是:http://www.wunderground.com/weather/api/d/docs?d=autocomplete-api devbridge 自动完成 git 是:https://github.com/devbridge/jQuery-Autocomplete 来自 wunderground 的示例响应是:http://autocomplete.wunderground.com/aq?c=US&format=jsonp&cb=myresults&query=san%20f

我已经不知所措几天了,我确定我正在寻找一些非常简单的东西。非常感谢任何帮助或指导。

【问题讨论】:

    标签: jquery json autocomplete wunderground


    【解决方案1】:

    要使其正常工作,您需要修改源代码,因为 jquery jsonp 默认回调查询字符串键不是“cb”,而是“回调”。所以在自动补全源码中添加:jsonp: 'cb'

            that.currentRequest = $.ajax({
                url: serviceUrl,
                data: params,
                type: options.type,
                jsonp: 'cb',
                dataType: options.dataType
            }).done(function (data) {
    

    那么你的代码应该是:

    var options, a;
    
    $(function(){
        options = {
            serviceUrl:'http://autocomplete.wunderground.com/aq?c=US&format=jsonp',
            minChars: 2,
            dataType : "jsonp",
            transformResult: function(response) {
                    console.log('response', response);
                    return {
                        suggestions: $.map(response.RESULTS, function(dataItem) {
                            return { value: dataItem.name, data: dataItem.zmw };
                    })
                };
            }
        };
        a = $('#autoLocation').autocomplete(options);
    });
    

    这对我来说很好。

    【讨论】:

    • 从未想过要修改源代码。非常感谢,单行完美! (很抱歉我不能投票,我还需要 4 个代表)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2016-04-30
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多