0

I am trying to use one webAPI coded in ASP.net but when I try to get data from that API using angular $http.get at the browser console I get GET <--URL--> Not found When I try to make requests to the same URL directly it works fine and returns data to me. I tried to add .json mimetype at Web.config in the ASP project but still same result.

The working url is: https://adaba.azurewebsites.net/api/flights/searchairports?name=london - note that it's a GET request with a required parameter so you need to pass a parameter to get something.

Here you can execute directly the api request from this Codepen: http://codepen.io/albpower/pen/mVgyNP

JS file:

var app = angular.module("APP",[]);

app.controller("appCtrl",function($http,$scope){
  $scope.calculate = function(){
    $http.get('http://adaba.azurewebsites.net/api/flights/searchairports',{
      name: $scope.input
    }).then(function (resp) {
     console.log(resp);
      $scope.response = resp;
  });
  }
})

HTML file:

<div ng-app="APP" ng-controller="appCtrl">
  <input type="text" placeholder="amount" ng-model="input"><br><br>

  <button ng-click="calculate()">Calculate</button>

  <pre>
  {{response | json}}
  </pre>
</div>
Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
albpower
  • 119
  • 3
  • 14
  • 1
    You have [Same Origin Policy](http://stackoverflow.com/questions/1489373/how-to-use-jquery-ajax-for-an-outside-domain) issue – Catalin Feb 17 '16 at 10:20
  • it can be a cors issue, read this: http://stackoverflow.com/questions/22546177/cross-domain-http-request-angularjs – Ricardo Pontual Feb 17 '16 at 10:23
  • Not a CORS issue: `Access-Control-Allow-Origin:*` in response header. – Johannes Jander Feb 17 '16 at 10:24
  • If your callback is not working then try with `$q` to resolve response – ojus kulkarni Feb 17 '16 at 10:26
  • sure it is not http://adaba.azurewebsites.net/api/flights/searchairport without at the end? –  Feb 17 '16 at 10:27
  • @JohannesJander the URL works that without 's' at the end is returning wrong data, The working url is: http://adaba.azurewebsites.net/api/flights/searchairports?name=london it's GET request with required parameter so you need to pass a parameter to get something – albpower Feb 17 '16 at 10:56
  • For clarification, I edited in your comment from below about the required parameter and the sample URL. – Johannes Jander Feb 17 '16 at 12:10

1 Answers1

2

You issue is:

  • you are not setting the parameters for the request correctly, you need to do this with the params property

The code should be:

 $http.get('https://adaba.azurewebsites.net/api/flights/searchairports',{
      params: {name: $scope.input}
 }).then(function (resp) {

See here: http://codepen.io/anon/pen/OMGpqX

Oh, and please use https://

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
  • the URL works that without 's' at the end is returning wrong data, The working url is: http://adaba.azurewebsites.net/api/flights/searchairports?name=london it's GET request with required parameter so you need to pass a parameter to get something PS: the problem is solved by putting params: before the name: input and it works, I didn't know that was required anyway thanks for your help and update your answer with the 's' at the url so I can choose your answer PPS: updated codepen: http://codepen.io/albpower/pen/mVgyNP – albpower Feb 17 '16 at 11:02