简体   繁体   English

当我指定JSON时,节点GET请求返回html?

[英]Node GET request returns html, when I specify JSON?

I am using the angular-fullstack, and I am trying to do a simple get request. 我正在使用angular-fullstack,并且尝试执行一个简单的get请求。 All relevant code below: 以下所有相关代码:

main.controller.js main.controller.js

this.$http.get('/'+"example").success(function(res){
        console.log(res);
    });

index.js index.js

router.get('/:city', controller.yelp);

user.controller.js user.controller.js

export function yelp(req, res, next){
  res.json({"foo": "bar"});
}

Yet, when I open up console in Google Chrome dev tools, I get the following: 但是,当我在Google Chrome开发者工具中打开控制台时,会得到以下信息:

<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <base href="/">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    <!-- build:css(client) app/vendor.css -->
      <!-- bower:css -->
      <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
      <!-- endbower -->
    <!-- endbuild -->
    <!-- build:css({.tmp,client}) app/app.css -->
      <link rel="stylesheet" href="app/app.css">
      <!-- injector:css -->
      <link rel="stylesheet" href="app/admin/admin.css">
      <link rel="stylesheet" href="app/app.css">
      <link rel="stylesheet" href="app/main/main.css">
      <link rel="stylesheet" href="components/footer/footer.css">
      <link rel="stylesheet" href="components/modal/modal.css">
      <!-- endinjector -->
    <!-- endbuild -->
  </head>
  <body ng-app="nightlifeApp">
    <!--[if lt IE 7]>
      <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <div ng-view=""></div>

    <!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-XXXXX-X');
      ga('send', 'pageview');
    </script>

    <!--[if lt IE 9]>
    <script src="bower_components/es5-shim/es5-shim.js"></script>
    <script src="bower_components/json3/lib/json3.min.js"></script>
    <![endif]-->
    <!-- build:js({client,node_modules}) app/vendor.js -->
      <!-- bower:js -->
      <script src="bower_components/jquery/dist/jquery.js"></script>
      <script src="bower_components/angular/angular.js"></script>
      <script src="bower_components/angular-resource/angular-resource.js"></script>
      <script src="bower_components/angular-cookies/angular-cookies.js"></script>
      <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
      <script src="bower_components/angular-route/angular-route.js"></script>
      <script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
      <script src="bower_components/lodash/dist/lodash.compat.js"></script>
      <script src="bower_components/angular-validation-match/dist/angular-validation-match.min.js"></script>
      <!-- endbower -->
    <!-- endbuild -->
    <!-- build:js(.tmp) app/app.js -->
      <script src="app/app.js"></script>
      <!-- injector:js -->
      <script src="app/admin/admin.module.js"></script>
      <script src="components/auth/auth.module.js"></script>
      <script src="components/util/util.module.js"></script>
      <script src="components/auth/auth.service.js"></script>
      <script src="app/admin/admin.controller.js"></script>
      <script src="app/account/account.js"></script>
      <script src="app/admin/admin.router.js"></script>
      <script src="app/app.constant.js"></script>
      <script src="app/main/main.controller.js"></script>
      <script src="app/main/main.js"></script>
      <script src="app/account/settings/settings.controller.js"></script>
      <script src="app/account/login/login.controller.js"></script>
      <script src="components/auth/interceptor.service.js"></script>
      <script src="components/auth/router.decorator.js"></script>
      <script src="components/auth/user.service.js"></script>
      <script src="components/footer/footer.directive.js"></script>
      <script src="components/modal/modal.service.js"></script>
      <script src="components/mongoose-error/mongoose-error.directive.js"></script>
      <script src="components/navbar/navbar.controller.js"></script>
      <script src="components/navbar/navbar.directive.js"></script>
      <script src="app/account/signup/signup.controller.js"></script>
      <script src="components/util/util.service.js"></script>
      <!-- endinjector -->
    <!-- endbuild -->
  </body>
</html>

Edit: This appears to be my index.html page 编辑:这似乎是我的index.html页面

The issue came from the definition of the routes. 问题来自路线的定义。 The route.js code is the following: route.js代码如下:

'use strict';

import errors from './components/errors';
import path from 'path';

export default function(app) {
  // Insert routes below
  app.use('/api/messages', require('./api/message'));
  app.use('/api/things', require('./api/thing'));
  app.use('/api/users', require('./api/user'));

  app.use('/auth', require('./auth'));

  // All undefined asset or api routes should return a 404
  app.route('/:url(api|auth|components|app|bower_components|assets)/*')
   .get(errors[404]);

  // All other routes should redirect to the index.html
  app.route('/*')
    .get((req, res) => {
      res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
    });
}

Thus, by simply adding /api/users to my request, I was able to be directed to the correct page. 因此,只需将/api/users添加到我的请求中,就可以将我定向到正确的页面。 (Note: Otherwise, we serve index.html, which explains why I was getting index.html). (注意:否则,我们将提供index.html,这解释了为什么我得到index.html)。

The successful request would be as follows: 成功的请求如下:

this.$http.get('/api/users/test').success(function(res){
        console.log(res);
    });

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

相关问题 节点get请求返回zip文件,需要将JSON导入MSSQL - Node get request returns zip file need to import JSON into MSSQL GET 请求返回 index.html doc 而不是 json 数据 - GET request returns index.html doc instead of json data 如何控制 HTML 页面在 GET 请求中返回的内容 - How do I control what a HTML page returns on a GET request 在 node.js 上发出获取请求时未定义的 json 字段 - undefined json field when making a get request on node.js HTTP GET请求(节点)返回501 - HTTP GET request (Node) returns 501 Swagger'GET'请求总是以text / html的形式返回响应和NOT application / json上的Accept类型 - Swagger 'GET' request always returns as text/html Accept type on response and NOT application/json JSON GET从代码/控制台调用时返回HTML,但从网址调用时返回JSON对象 - JSON GET returns HTML when called from code/console but returns JSON object when called from web address 我想从HTML文档中获取文本框值,并在节点JS中使用Json文件按下提交按钮时发布它们 - I want to get my textbox values from HTML document and post those when submit button is pressed using Json file in node JS 向节点 js 发送发布请求时,我得到一个空的 object - when sending a post request to node js, I get an empty object Ajax GET 请求返回当前 HTML 页面 - Ajax GET request returns current HTML page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM