【问题标题】:$http get returns not found,while the direct request works good$http get 返回未找到,而直接请求效果很好
【发布时间】:2016-05-28 23:38:01
【问题描述】:

我正在尝试使用一个在 ASP.net 中编码的 webAPI,但是当我尝试在浏览器控制台上使用 angular $http.get 从该 API 获取数据时,我得到 GET Not found 当我尝试直接向同一个 URL 发出请求时,它可以正常工作并向我返回数据。我尝试在 ASP 项目的 Web.config 中添加 .json mimetype,但结果仍然相同。

有效的 url 是:https://adaba.azurewebsites.net/api/flights/searchairports?name=london - 请注意,这是一个带有 必需参数的 GET 请求,因此您需要传递一个参数来获取某些内容。

这里可以直接执行来自这个Codepen的api请求:http://codepen.io/albpower/pen/mVgyNP

JS 文件:

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 文件:

<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>

【问题讨论】:

标签: javascript html angularjs


【解决方案1】:

你的问题是:

  • 您没有正确设置请求的参数,您需要使用params 属性进行此操作

代码应该是:

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

请看这里:http://codepen.io/anon/pen/OMGpqX

哦,请使用https://

【讨论】:

  • 该 URL 工作,没有 's' 最后返回错误数据,工作 url 是:adaba.azurewebsites.net/api/flights/searchairports?name=london 它是带有必需参数的 GET 请求,因此您需要传递一个参数来获取一些东西 PS:问题是通过在名称之前放置参数来解决的:输入并且它可以工作,但我不知道这是必需的,感谢您的帮助并在网址上使用“s”更新您的答案,以便我可以选择您的答案 PPS:更新代码笔:codepen.io/albpower/pen/mVgyNP
猜你喜欢
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
相关资源
最近更新 更多