Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to implement Typeahead using my Web Api controller by adopting to this code, that works fine:

HTML

<div class='container-fluid' ng-controller="TypeaheadCtrl2">
    <pre>Model: {{result | json}}</pre>
    <input type="text" ng-model="result" typeahead="suggestion for suggestion in cities($viewValue)">
</div>

Controller in app.js

myApp.controller('TypeaheadCtrl2', function ($scope, $http, limitToFilter) {

    //http://www.geobytes.com/free-ajax-cities-jsonp-api.htm

    $scope.cities = function (cityName) {
        return $http.jsonp("http://gd.geobytes.com/AutoCompleteCity?callback=JSON_CALLBACK &filter=US&q=" + cityName).then(function (response) {
            return limitToFilter(response.data, 15);
        });
    };
});

However when I change return $http.jsonp("http://gd.geobytes.com/AutoCompleteCity?callback=JSON_CALLBACK &filter=US&q=" + cityName) to call my own WebApi controller return $http.jsonp("api/airports/" + cityName) it stops working.

But if I call my Web Api directly, like http://mysite:80/api/airports/los it returns this json:

["San Martin DeLos Andes (CPC)","San Carlos DeBariloche (BRC)","Los Menucos (LMD)","Paso De Los Libres (AOL)","Barbelos (BAZ)","Los Angeles (LSQ)","Los Chiles (LSL)","Mali Losinj (LSZ)","Milos (MLO)","Volos (VOL)","Paso Caballos (PCG)","Los Mochis (LMM)","Vilanculos (VNX)","San Carlos (NCR)","Lagos (LOS)","Losuia (LSA)","Lossiemouth (LMO)","Los Angeles (JID)","Los Angeles (JBP)","Los Alamos (LAM)","Los Angeles (LAX)","Los Banos (LSN)","Lost Harbor (LHB)","Lost River (LSR)","San Carlos (SQL)","Los Angeles, CA (VNY)","Los Angeles (WHP)","Los Roques (LRV)"]

which is in exactly the same format as returned by http://gd.geobytes.com/AutoCompleteCity?filter=US&q=los:

["Los Alamitos, CA, United States","Los Alamos, CA, United States","Los Alamos, NM, United States","Los Altos, CA, United States","Los Angeles, CA, United States","Los Banos, CA, United States","Los Ebanos, TX, United States","Los Fresnos, TX, United States","Los Gatos, CA, United States","Los Indios, TX, United States","Los Lunas, NM, United States","Los Molinos, CA, United States","Los Ojos, NM, United States","Los Olivos, CA, United States","Los Osos, CA, United States","Losantville, IN, United States","Lost City, WV, United States","Lost Creek, KY, United States","Lost Creek, PA, United States","Lost Creek, WV, United States"]

Please advise.

share|improve this question
    
Are your Web API and the angularjs app on the same domain? –  runTarm Aug 14 '14 at 16:18
    
Yes they are on the same domain. –  superconsultant Aug 14 '14 at 17:09

3 Answers 3

up vote 0 down vote accepted

There is no need to use JSONP if your Web Api is on the same domain as your angularjs application (and your Web Api doesn't seems to support JSONP anyway).

You could just use a simple GET request like this:

$scope.cities = function (cityName) {
    return $http.get("api/airports/" + cityName).then(function (response) {
        return limitToFilter(response.data, 15);
    });
};

Hope this helps.

share|improve this answer
    
Just copied and pasted your code and BOOM, it works! Thanks. –  superconsultant Aug 14 '14 at 17:21

Try to use ´$http.get´ separate the promise from the assingment of ´$scope.cities´:

myApp.controller('TypeaheadCtrl2', function ($scope, $http, limitToFilter) {

    $http.get("http://gd.geobytes.com/AutoCompleteCity?callback=JSON_CALLBACK &filter=US&q=" + cityName).then(function (response) {
        $scope.cities = limitToFilter(response.data, 15);
    });
});
share|improve this answer
    
Thanks man. Solution above has solved my problem, I believe it similar to yours. Just more refined. I am starting to get a handle on Angular, but still far from decent. –  superconsultant Aug 14 '14 at 17:27
    
Oh yeah they are both equivalent, I just prefer not to mix the things with various functions and returns one inside another, javascript can be very confusing sometimes lol. A nice thing you can lookup next is how to create a service and put all your $http requests in it, so your controllers would not need the $http nor the endpoint address. –  Fedaykin Aug 14 '14 at 17:40
    
Thanks again. My recent goal is to establish proof of concept scenarios for all the Angularjs controls (suggestion lists, dropdowns, calendars, routing , etc) , so I can start converting my WebForms app to Angular app with mini spa's. Any Examples you can point me to for modal pop-ups with search capabilities using Angular? –  superconsultant Aug 14 '14 at 18:17

I think your typeahead is expecting a JSON object. And your API is returning javascript array. It should return possibly in below format:

{['CITY1', 'CITY2', 'CITY3']}

share|improve this answer
    
But as I mentioned above if I run web api directly in my url address it writes to the page exactly the same string as Google's gd.geobytes.com/AutoCompleteCity web service writes to the page. –  superconsultant Aug 14 '14 at 17:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.