Yelp API v3通过电话搜索

时间:2018-09-04 18:40:04

标签: jquery api yelp yelp-fusion-api

我需要能够通过电话找到业务。我找到了可以执行搜索的代码示例,并且效果很好,但是当我将myurl更改为通过电话搜索时-即使返回结果,也不会返回任何结果...

我想念什么?

<div id="results"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=sushi&location=austin";
// var myurl = "https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/phone?phone=+15122750852";

$.ajax({
    url: myurl,
    headers: {
        'Authorization':'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    },
    method: 'GET',
    dataType: 'json',
    success: function(data){
        // Grab the results from the API JSON return
        var totalresults = data.total;
        // If our results are greater than 0, continue
        if (totalresults > 0){
            // Display a header on the page with the number of results
            $('#results').append('<h5>We discovered ' + totalresults + ' results!</h5>');
            // Itirate through the JSON array of 'businesses' which was returned by the API
            $.each(data.businesses, function(i, item) {
                // Store each business's object in a variable
                var id = item.id;
                var alias = item.alias;
                var phone = item.display_phone;
                var image = item.image_url;
                var name = item.name;
                var rating = item.rating;
                var reviewcount = item.review_count;
                var address = item.location.address1;
                var city = item.location.city;
                var state = item.location.state;
                var zipcode = item.location.zip_code;
                // Append our result into our page
                $('#results').append('<div id="' + id + '" style="margin-top:50px;margin-bottom:50px;"><img src="' + image + '" style="width:200px;height:150px;"><br>We found <b>' + name + '</b> (' + alias + ')<br>Business ID: ' + id + '<br> Located at: ' + address + ' ' + city + ', ' + state + ' ' + zipcode + '<br>The phone number for this business is: ' + phone + '<br>This business has a rating of ' + rating + ' with ' + reviewcount + ' reviews.</div>');
            });
        } else {
            // If our results are 0; no businesses were returned by the JSON therefor we display on the page no results were found
            $('#results').append('<h5>We discovered no results!</h5>');
        }
    }
});

</script>

1 个答案:

答案 0 :(得分:1)

我认为您的端点不支持V3;我已经更新了您的代码。请尝试以下操作:

function selectFunc( event, ui ) {
     event.preventDefault();

    // Set the uiObject's Label (this comes from the SearchPerson query) into the text box
    $("#mybox").val(ui.item.fullName);
    // ... some other tasks
}

别忘了更新应用程序的API密钥。

您的URI:https://api.yelp.com/v3/businesses/phone?phone=+15122750852应该是https://api.yelp.com/v3/businesses/search/phone?phone=+15122750852;代码中发生的事情是您由于缺少服务而陷入了死胡同的终点。 :)