【问题标题】:Autocomplete input suggestions自动完成输入建议
【发布时间】:2020-08-21 04:03:45
【问题描述】:

我正在尝试使用自动完成地点列表设置输入。我想使用 Here autosuggest 工具。

https://developer.here.com/documentation/geocoding-search-api/dev_guide/topics/endpoint-autosuggest-brief.html

为此我做了这个代码:

<div class="autocomplete input-group has-warning">
        <input id="search-where" name="w" type="text" class="form-control input-lg" placeholder="A quel endroit?" required="required" value="" autocomplete="on" onkeyup="mySearch(this.value)" />
        <span class="input-group-btn">
            <button type="submit" class="btn btn-lg btn-warning"><span class="glyphicon glyphicon-screenshot"></span></button>
        </span>
    </div>

    {literal}
        <script type="text/javascript">

            function mySearch(e){
            $.ajax({
                url: "https://places.ls.hereapi.com/places/v1/suggest",
                type: 'GET',
                data: {
                    at: '44.771079,5.742806',
                    q: 'savoie',
                    app_id: '2FXOZOY',
                    app_code: 'b_9sMKgQmSjWUj1rlyY0wI'
                  },
                headers : { "Authorization": "Bearer" + $('b_9sMKgQ5qzJF0SusExJJx9irrHHimSjWUj1rlyY0wI').val()},
                  beforeSend: function(xhr){
                    xhr.setRequestHeader('Accept', 'application/json');
                  },
                  success: function (data) {
                    alert(JSON.stringify(data));
                  }
            });
            $( "#search-where" ).autocomplete({
                source: mySearch 
            });
        }               
        </script>
    {/literal}

但是当我尝试在输入中写入时出现此错误:

我显然完全迷失了......如果有人可以帮助我,我将不胜感激。 无论如何,感谢您阅读我的文章

【问题讨论】:

    标签: html input autocomplete xmlhttprequest here-api


    【解决方案1】:

    我认为您正在对与您所在页面不同的 URL 执行 XMLHttpRequest。在这种情况下,由于安全原因,浏览器会完全阻止它并给出 CORS 错误。有很多方法可以设置它。此处提供tutorial

    【讨论】:

    • 感谢您抽出宝贵时间回答我,我要试试这个
    猜你喜欢
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多