如何在localhost上托管Web应用程序和REST API?

时间:2016-10-04 09:58:39

标签: javascript ajax asp.net-web-api

我正在React中开发一个SPA,它通过我正在构建的ASP.NET Web Api与我的数据库进行通信。

我在localhost:8080上使用webpack托管React应用,并且网络API默认为localhost:56963

当我尝试从我的React客户端发出GET请求时,我得到了一个奇怪的结果。而不是从webapi返回数据,它返回客户端的index.html内容。 在这里,我在我的React客户端中调用webapi:

  componentWillMount(){
    // I expect this to return the resource with ID = 1.
    $.ajax({
      url: "http://localhost:56963/api/biologicalresource/1" ,
      cache: false,
      success: function(result){
        console.log(result);
      }
    });
  }

的console.log(结果):

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="styling.css">
  </head>
  <body>
    <div id="app"></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="bundle.js"></script>
  </body>
</html>

但是当我在Powershell中运行wget "http://localhost:56963/api/biologicalresource/1"时,我得到了预期的结果:

StatusCode        : 200
StatusDescription : OK
Content           : {"Name":"Pontus","Skills":["HTML","C#","JavaScript","ReactJs","SCRUM","Web Api","REST"]}
RawContent        : HTTP/1.1 200 OK
                    Pragma: no-cache
                    X-SourceFiles: =?UTF-8?B?Qzpcc2hhcmluZ2lzY2FyaW5nXFNlcnZlclxBUElcQVBJXGFwaVxiaW9sb2dpY2FscmVzb3VyY2VzXDE=?=
                    Content-Length: 88
                    Cache-Control: no-cache
                    Content-Typ...
Forms             : {}
Headers           : {[Pragma, no-cache], [X-SourceFiles, =?UTF-8?B?Qzpcc2hhcmluZ2lzY2FyaW5nXFNlcnZlclxBUElcQVBJXGFwaVxiaW9sb2dpY2FscmVzb3VyY2VzXDE=?=], [Content-Length, 88], [Cache-Control
                    , no-cache]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 88

我做错了什么?

0 个答案:

没有答案