【问题标题】:AJAX Request to read JSON object from node.js/express server从 node.js/express 服务器读取 JSON 对象的 AJAX 请求
【发布时间】:2016-10-02 14:16:03
【问题描述】:

我设置了一些服务器端代码来向外部网站发出请求并返回一个 JSON 对象,然后将其传递给我的 node.js/express 应用程序的客户端。然后我打算在客户端对 JSON 执行一些额外的处理。

在我的 index.js 文件中,我定义了以下代码来呈现我的视图并对外部网站进行 RESTful 调用:

var express = require('express');
var router = express.Router();
var request = require('request');

// Urls for external server REST API function calls
var url = 'https://someserver.com/appserver/portal/api/1.0/results/recent';

/* GET list of recent reports */
router.get('/testapi', function(req, res, next) {
  res.render('testapi', { title: 'List Recent Reports' });
});

/* TEST: function to GET report list */
router.get('/recentreports', function(req, res){
  request({
    url: url,
    json: true
  }, function (error, response, body) {
      if (!error && response.statusCode === 200) {
        res.send(body); // Send the response to the client
    } else {
        console.log(error);
    }
  })
});


module.exports = router;

在我的翡翠视图中,我设置了一个按钮来调用 /recentreports 函数并发出请求(可能是 AJAX 请求?)以从服务器接收 JSON 对象。

我的问题是:如何将服务器的响应读入客户端应用程序,以便处理 JSON?

我的玉观如下:

extends layout

head

block content
  h1= title
  p Welcome to #{title}
  script.
    // not sure what goes here
    $(function()){
      $('#getRecentReports').on('', function() {
          $.get('recentreports', function(body) {
            $('#jsonbody').html(body)
          })
        })
    }

  button(
    onclick = "getRecentReports()" 
  )

编辑:在使用它之后更新了 .jade 代码

【问题讨论】:

  • 旁注:在服务器上,你不能用request({url: url, json: true}).pipe(res)吗?
  • 我想可能有,所以我的服务器端代码如下所示: ... router.get('/recentreports', function(req, res){ request({url: url, json : true}).pipe(res) }); ...如果我进行更改,这将如何影响我在响应中读取的方法?
  • 它不应该改变它,这就是为什么我把它作为附注提供。键入它比显式提供回调并在您将响应传递到其他地方时完全缓冲请求更简单。
  • 我明白了,当我完成剩下的工作时,我会尝试这样做。感谢您的提示。

标签: javascript ajax node.js express pug


【解决方案1】:

我的推荐建议:

script.
  $(function() {
    $('#getRecentReports').on('click', function() {
      $.get('recentreports', function(body) {
        $('#jsonbody').html(body)
      });
    });
  });

button(
  id = "getRecentReports"
)

渲染:

<script>
  $(function() {
    $('#getRecentReports').on('click', function() {
      $.get('recentreports', function(body) {
        $('#jsonbody').html(body)
      });
    });
  });

</script>
<button id="getRecentReports"></button>

或者:

script.
  function getRecentReports() {
    $.get('recentreports', function(body) {
      $('#jsonbody').html(body)
    });
  }

button(
  onclick = "getRecentReports()"
)

渲染:

<script>
  function getRecentReports() {
    $.get('recentreports', function(body) {
      $('#jsonbody').html(body)
    });
  }

</script>
<button onclick="getRecentReports()"></button>

【讨论】:

  • 我尝试了你的第一个建议并得到了这个错误:Uncaught ReferenceError: $ is not defined。我安装了 jquery npm 模块,它有两个依赖项 jsdom 和 xmlhttprequest (我最初忘了做)。我还在脚本中添加了以下内容: var $ = require('jQuery');然后抛出此错误: testapi:2 Uncaught ReferenceError: $ is not defined。我认为我引用 jQuery 模块的方式有问题。有什么建议吗?
  • 阅读一些关于 node 的一般教程以及如何使用 express 提供静态请求,这确实是您似乎难以理解的基本内容。您不太清楚 require 行的位置。节点和客户端 JavaScript 不共享资源,因此在一侧定义变量不允许另一侧访问它。如果您想要最简单的方法,我的建议是将script(src="url to jquery cdn") 包含在head 的jade 文件中,这样您就不必从npm 模块中使用express serve jquery。
猜你喜欢
  • 1970-01-01
  • 2012-01-28
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2016-04-17
相关资源
最近更新 更多