简体   繁体   English

如何将 javascript 对象文字从 Node 后端发送到浏览器前端?

[英]How to send javascript object literals from a Node backend to the browser frontend?

I am using Node where I have JavaScript object literals with methods in the backend, eg:我正在使用 Node,在那里我有 JavaScript 对象文字和后端的方法,例如:

const report = {
    id: 1,
    title: 'Quarterly Report for Department 12345',
    abstract: 'This report shows the results of the sales and marketing divisions.', 
    searchText: function () {
        return this.title + '|' + this.abstract;
    }
};

And I want to send these object literals to the frontend via AJAX and be able to call the methods on these objects as I can in the backend.我想通过 AJAX 将这些对象文字发送到前端,并且能够像在后端一样调用这些对象上的方法。

But even though I can send the objects to the frontend without JSON.stringify(), they are still converted to plain JSON by the time they reach my frontend:但是即使我可以在没有 JSON.stringify() 的情况下将对象发送到前端,当它们到达我的前端时,它们仍然会转换为纯 JSON:

在此处输入图片说明

Am I missing something, or is there not a way to send full object literals from backend to frontend.我是否遗漏了什么,或者没有办法将完整的对象文字从后端发送到前端。 I'm using Axios.我正在使用 Axios。

But even though I can send the objects to the frontend without JSON.stringify(),但即使我可以在没有 JSON.stringify() 的情况下将对象发送到前端,

It sounds like you are using JSON.stringify … just indirectly (via a library).听起来您正在使用JSON.stringify ……只是间接地(通过库)。

JSON has no function data type. JSON 没有function数据类型。 So you can't just use JSON.所以你不能使用 JSON。

You have a few options.你有几个选择。

Listed in the order I'd recommend them in.按照我推荐的顺序列出。


Resolve the methods解决方法

In your example, your function simple return this.title + '|' + this.abstract;在您的示例中,您的函数简单return this.title + '|' + this.abstract; return this.title + '|' + this.abstract; so you could replace it with a string:所以你可以用一个字符串替换它:

const report = {
    id: 1,
    title: 'Quarterly Report for Department 12345',
    abstract: 'This report shows the results of the sales and marketing divisions.', 
    searchText: 'Quarterly Report for Department 12345|This report shows the results of the sales and marketing divisions.'
    }
};

You could use the replacer argument of JSON.stringify to do this automatically for any method on the object.您可以使用JSON.stringifyreplacer参数为对象上的任何方法自动执行此操作。

This is the simplest option but results in data that doesn't update dynamically so it might not be suitable for your needs.这是最简单的选项,但会导致数据不会动态更新,因此可能不适合您的需求。

Add the methods with client-side code添加带有客户端代码的方法

Send a simple object which doesn't have the method but does have a field which describes the type of object it does.发送一个简单的对象,它没有方法但确实有一个描述它所执行的对象类型的字段。

Then inflate it on the client:然后在客户端对其进行充气:

const type = ajaxResponse.type;
const Constructor = collectionOfConstructorFunctions[type];
const data = new Constructor(ajaxResponse.data);

Send JavaScript instead改为发送 JavaScript

You could use JSONP instead of Axios.您可以使用JSONP而不是 Axios。

The response would be application/javascript instead of application/json so you could encode function expressions in the returned data.响应将是application/javascript而不是application/json因此您可以在返回的数据中编码函数表达式。

I don't recommend this option.我不推荐这个选项。

Encode the functions in the JSON and then include them client-side对 JSON 中的函数进行编码,然后将它们包含在客户端

This is horrible.这太可怕了。

const report = {
    id: 1,
    title: 'Quarterly Report for Department 12345',
    abstract: 'This report shows the results of the sales and marketing divisions.',
    searchText: "return this.title + '|' + this.abstract;"
}

and then, on the client:然后,在客户端上:

report.searchText = new Function(report.searchText);
console.log(report.searchText());

This is effectively using eval .这是有效地使用eval Don't do it.不要这样做。

暂无
暂无

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

相关问题 如何在节点中将多个查询结果从后端发送到前端 - How to send multiple query results from backend to frontend in node 如何从后端获取角度数据并通过node.js发送给前端? - How get data with angular from the backend and send it with node.js for the frontend? 如何使用 StreamingResponse 将多个图像从 FastAPI 后端发送到 JavaScript 前端? - How to send multiple images from FastAPI backend to JavaScript frontend using StreamingResponse? 在前端 JS 中使用 object 来自 Node.js / Express 后端 - Use object from Node.js / Express backend in frontend JS 如何在 javascript 前端使用 async await 从 async await node.js 后端接收数据 - how to use async await on javascript frontend to receive data from async await node.js backend 如何从 PHP 后端接收 JSON object 并将前端转换为 Z686155AF75A60A0F6E9D80E1 数组 - How do I receive a JSON object from PHP backend and convert to JavaScript array for frontend 如何从javascript中的Object文字中提取注释 - How to extract comments from Object literals in javascript 如何将数据从html(Django后端)发送到js(Vue前端) - How to send data from html (Django backend) to js (Vue frontend) 如何安全地从后端发送图像以显示在前端 HTML? - How to securely send image from backend to be displayed in frontend HTML? 如何从前端向后端快速发送数据(url 字符串)? - How to send data (a url string) in express to backend from frontend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM