简体   繁体   English

来自Angular JS的HTTP Get请求收到空回复

[英]HTTP Get request from Angular JS receives empty reply

Since I am fairly new to web development, this might very well be a very easy to solve question. 由于我是Web开发的新手,因此这很可能很容易解决。 Unfortunately I am not able to solve this myself as I have no clue on where to search. 不幸的是,我自己无法解决此问题,因为我不知道在哪里搜索。

Currently I'm trying to get data out of an ASP.NET Web API that is running on my local machine. 目前,我正在尝试从本地计算机上运行的ASP.NET Web API中获取数据。 To get this data I've written a small piece of JavaScript using AngularJS: 为了获得这些数据,我使用AngularJS编写了一小段JavaScript:

function MainController($scope, $http) {
  $http.get("http://localhost/DBLayerDLL/api/tablets/1005").
    then(function(response) {$scope.data=response;},
         function(response){$scope.data=response;});
}

The HTML page that is used, is also very simple: 使用的HTML页面也非常简单:

<!DOCTYPE html>
<html ng-app>

  <head>
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <script src="scripts/main.js"></script>
  </head>

  <body ng-controller="MainController">
    <h1>Hello World!</h1>
    <div>
      <div>Data: {{data}}</div>
    </div>
  </body>

</html>

The action of the controller in my Web API is implemented as follows: Web API中控制器的操作实现如下:

[RoutePrefix("api/tablets")]
public class TabletsController : ApiController
{
    [Route("{ID}")]
    [HttpGet]
    public TabletRecord GetTablet(int ID)
    {
        TabletRecord tablet = new TabletRecord();
        tablet.LoadFromID(ID);
        return tablet;
    }
}

The problem lies within the response I get from the HTTP request. 问题出在我从HTTP请求获得的响应之内。 When printed on the page it looks like this: 当打印在页面上时,它看起来像这样:

{"data":"","status":0,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://localhost/DBLayerDLL/api/tablets/1005","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

Which is not correct, as I know that my API will return data. 这是不正确的,因为我知道我的API将返回数据。 It gets even weirder for me when I use Fiddler to see what is going on. 当我使用Fiddler看看发生了什么时,这对我来说甚至变得更加奇怪。 Within Fiddler I can see the request and see that the response is completely different from what I see on my web page: 在Fiddler中,我可以看到请求,并且看到响应与我在网页上看到的完全不同:

Fiddler HTTP请求

Due to my lack of knowledge and experience I don't know what is going on... 由于缺乏知识和经验,我不知道发生了什么...

  • Is there any thing I am doing wrong in my JavaScript? 我的JavaScript有什么错吗?
  • Should I do something else within the HTML page? 我应该在HTML页面中做其他事情吗?
  • Or is there any special thing that I need in my Web API? 还是我的Web API中需要任何特殊的东西?

Any help or thoughts are highly appreciated! 任何帮助或想法都将受到高度赞赏!

After spending quite some more time on this I've found a couple of things were needed to solve my problem: 在花了更多时间之后,我发现需要一些事情来解决我的问题:

  1. I've noticed that I had to enable CORS on my Web API in order for the JavaScript to be able to make requests to my Web API. 我注意到,为了使JavaScript能够向我的Web API发出请求,我必须在Web API上启用CORS。 I've came to this via this link , I just followed the steps as explained to get it up and running. 我是通过此链接进入此页面的,我只是按照说明的步骤进行安装和运行。

  2. I've also discovered at first that I can also get my data using the general XMLHttpRequest object available in JavaScript. 我还首先发现,我还可以使用JavaScript中可用的常规XMLHttpRequest对象获取数据。 By first getting my data to display using this method, I've also discovered the next item: 通过首先使用此方法显示数据,我还发现了下一项:

  3. The response object returned by the $http.get method is actually the object that is directly returned by my Web API. $http.get方法返回的响应对象实际上是我的Web API直接返回的对象。 So I can just use the defined properties to display the required data. 因此,我只能使用定义的属性来显示所需的数据。

On normal php i get the angular variables with 在普通的PHP上,我得到的角度变量

$data = file_get_contents("php://input");

you should look if 你应该看看

print_r($data);

contains what u need. 包含您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM