javascript - 我的 foursquare api 调用出了什么问题?

标签 javascript ajax foursquare

现场示例在这里 http://kenziejoy.github.io/frontend-nanodegree-map/

我正在尝试提取有关我在数组中硬编码的位置的数据 - 通过它们的 foursquare id (似乎不起作用)或它们的纬度和经度。 (客户端 ID 和密码是变量,我只是没有在此处显示它们)

除了从数据库中提取数据以显示在 map 上之外,我不需要任何其他功能,因此我认为它会属于无用户访问范围,但它给了我一个错误,即请求是错误的,因为我不这样做有适当的身份验证。

提前致谢

来自 foursquare 网站 “无用户访问

我们的一些与特定用户信息无关的端点(例如 field 搜索)启用了无用户访问(这意味着您不需要对您的应用程序进行访问验证)。要发出无用户请求,请在请求 URL 中指定使用者 key 的客户端 ID 和 key ,而不是身份验证 token 。

https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=YYYYMMDD

要查看每个端点需要什么级别的权限,请查看端点页面顶部的过滤器。”

        /**********FourSquare***************/
    $.ajax({
        url:'https://api.foursquare.com/v2/venues/search',
        dataType: 'json',
        data: 'limit=1' +
                '&ll='+ placeItem.lat() +','+ placeItem.lng() +
                '&?client_id='+ CLIENT_ID +
                '&client_secret='+ CLIENT_SECRET +
                '&v=20140806' +
                '&m=foursquare',
        async: true,

        success: function (data) {
            var result = data.response.venue;
            var contact = result.hasOwnProperty('contact') ? result.contact : '';
            if (contact.hasOwnProperty('formattedPhone')) {
                placeItem.phone(contact.formattedPhone || '');
            }

            var location = result.hasOwnProperty('location') ? result.location : '';
            if (location.hasOwnProperty('address')) {
                placeItem.address(location.address || '');
            }

            var bestPhoto = result.hasOwnProperty('bestPhoto') ? result.bestPhoto : '';
            if (bestPhoto.hasOwnProperty('prefix')) {
                placeItem.photoPrefix(bestPhoto.prefix || '');
            }

            if (bestPhoto.hasOwnProperty('suffix')) {
                placeItem.photoSuffix(bestPhoto.suffix || '');
            }

            var description = result.hasOwnProperty('description') ? result.description : '';
            placeItem.description(description || '');

            var rating = result.hasOwnProperty('rating') ? result.rating : '';
            placeItem.rating(rating || 'none');

            var url = result.hasOwnProperty('url') ? result.url : '';
            placeItem.url(url || '');

            placeItem.canonicalUrl(result.canonicalUrl);

            // Infowindow code is in the success function so that the error message

            // Content of the infowindow
            var contentString = '<div id="iWindow"><h4>' + placeItem.name() + '</h4><div id="pic"><img src="' +
                    placeItem.photoPrefix() + '110x110' + placeItem.photoSuffix() +
                    '" alt="Image Location"></div><p>Information from Foursquare:</p><p>' +
                    placeItem.phone() + '</p><p>' + placeItem.address() + '</p><p>' +
                    placeItem.description() + '</p><p>Rating: ' + placeItem.rating() +
                    '</p><p><a href=' + placeItem.url() + '>' + placeItem.url() +
                    '</a></p><p><a target="_blank" href=' + placeItem.canonicalUrl() +
                    '>Foursquare Page</a></p><p><a target="_blank" href=https://www.google.com/maps/dir/Current+Location/' +
                    placeItem.lat() + ',' + placeItem.lng() + '>Directions</a></p></div>';

            // Add infowindows
                google.maps.event.addListener(placeItem.marker, 'click', function () {
                infowindow.open(map, this);
                // Bounce animation
                placeItem.marker.setAnimation(google.maps.Animation.BOUNCE);
                setTimeout(function () {
                    placeItem.marker.setAnimation(null);
                }, 800);
                infowindow.setContent(contentString);
            });
    },

        // Alert the user on error.
        error: function (e) {
            infowindow.setContent('<h5>Foursquare data is unavailable.</h5>');
            document.getElementById("error").innerHTML = "<h4>Foursquare data is unavailable. Please try refreshing.</h4>";
    }
    });

最佳答案

我查看了实时示例 URL,您在 Chrome 的 JavaScript 控制台中收到了很多错误的请求错误。

看看这些,您有一个错误的 URL,您正在使用:

https://api.foursquare.com/v2/venues/search?limit=1&ll=45.5589522,-122.6517163&?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&v=20140806&m=foursquare

问题似乎在于您:

 &?client_id

这使得 URL 无效。

将其更改为

 &client_id

修复了这个问题,然后我就可以看到从 Foursquare 返回的数据。

关于javascript - 我的 foursquare api 调用出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35026964/

相关文章:

javascript - Phonegap 没有运行 javascript

javascript - 如何排除某些链接上的 JQuery/Javascript 淡入淡出功能?

javascript - 为什么 'this' 在调试器中未定义但可在 console.log 中打印?

javascript - 尝试在 CRM 2011 UR 12 中使用 XRMServiceToolkit 反序列化消息时出错

ruby-on-rails - Rails ajax 表单语法错误

PHP - 从 AJAX 调用的脚本将数据记录到文件时出现问题

php - Ajax 从 php 获取返回值?

google-maps - 如何为谷歌地图标记添加背景图片?

iphone - 我的 foursquare 身份验证、callback_url 和应用程序网站是什么?

foursquare - 搜索端点开始返回组