简体   繁体   English

将数组从节点服务器发送到js脚本

[英]Send an array from node server to js script

My server (node.js) maintains an array : 我的服务器(node.js)维护一个数组:

var list = [];

I want to use this array in some js code (client side). 我想在一些js代码(客户端)中使用此数组。 I would like to retrieve it thanks to ajax. 由于ajax,我想找回它。 What is the best practice? 最佳做法是什么?

$.ajax({
    url: "http://localhost:8000/updatePendingAlerts",
    timeout: 2000,
    success: function (data)  {
      console.log(data);
      //data should be an array

    },
    error: function(jqXHR, textStatus, errorThrown) {
      clearInterval(timeout);
      alert('error ' + textStatus + " " + errorThrown);
    }
  });

Serialise it to JSON (with JSON.stringify ) and output it with an application/json content-type header. 将其序列化为JSON(使用JSON.stringify ),然后将其与application/json内容类型标头一起输出。

It will then be an array in data with the client side JavaScript you already have. 然后它将是一个data数组,其中包含您已有的客户端JavaScript。

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

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