简体   繁体   English

我可以将基于RESTful api的服务器端的初始动态数据传递到基于角度的前端页面吗?

[英]Can I pass along initial dynamic data from a RESTful api based server side, to an angular based front end page?

I have repeatedly searched for, and come across questions and answers on the possibilities of accessing session-, and requestscoped data with client side javascript or similar. 我已经反复搜索并遇到有关使用客户端javascript或类似内容访问会话和请求范围数据的可能性的问题和答案。 The answer seems to be a consistent "no you can't do that". 答案似乎是一致的“不,你不能那样做”。 But the Front Controller pattern, as described here , forwards the java objects for request and response, and exposes them for retrieval in the browser, through Expression Language. 但是, 如此处所述 ,前端控制器模式转发Java对象以进行请求和响应,并通过表达式语言将其公开以在浏览器中进行检索。

To me this seems like a great approach, as it passes along data at the same time as the redirect. 对我来说,这似乎是一种很好的方法,因为它在重定向的同时传递数据。 In other words, if the web page has a table that should be populated with default, but currently updated data (say weather report for next week). 换句话说,如果网页上有一个表,该表应填充有默认值,但当前已更新数据(例如下周的天气报告)。 All other approaches seems to include loading the website first, and then loading data to populate, through ajax or similar. 所有其他方法似乎都包括先加载网站,然后再通过ajax或类似方法加载要填充的数据。

I've been starting to look at angular, and RESTful api's, and I am having a hard time grasping even the basics. 我已经开始研究有角度的和RESTful api了,甚至很难掌握基础知识。 One of the upsides of angular, should be it's ability to divide front side code into modules, and include template html's, without loading them seperately. angular的优点之一是能够将前端代码分为模块,并包含html模板,而无需分别加载它们。 But it seems like in a RESTful api, I can't pass along dynamic initial values to be displayed on a page. 但是似乎在RESTful api中,我无法传递动态初始值以显示在页面上。

So with a RESTful api structure, am I right to assume that I need to load the page first, and then retrieve the data through the $http-service? 因此,使用RESTful api结构,我是否应该假设我需要先加载页面,然后通过$ http-service检索数据? And if so, why is it designed, so that I need to make a double (or multiple) request to the server? 如果是这样,为什么要设计它,所以我需要对服务器进行两次(或多次)请求? If not, how can I pass data to the page, as the server redirects to it? 如果没有,当服务器重定向到页面时,如何将数据传递到页面? Am I wrong to use redirects in the first place? 首先使用重定向会错吗?

Know this is an old question, and not certain I am the best man to answer this question, but as I am struggling a little bit with this (or a similar question). 知道这是一个古老的问题,不确定我是否是回答这个问题的最佳人选,但是由于我对此有点挣扎(或类似的问题)。

Answer to your first question is probably more to do with rendering and how web browsers do things. 第一个问题的答案可能与渲染以及Web浏览器的工作方式有关。 (I may be wrong, so feel free to tell me off). (我可能是错的,请随时告诉我)。

When the server receives a http request (ie visitor opening www.yourpage.com) it will start sending data streams. 当服务器收到http请求(即访问者打开www.yourpage.com)时,它将开始发送数据流。 Depending on how your webpage, something along the lines of index.html to start with, in index.html you will have a number of hooks such as maybe a section.html, css files, pictures and javascript files. 根据您的网页的方式,在index.html中,类似index.html开头的内容会包含许多钩子,例如section.html,css文件,图片和javascript文件。

Once the javascript files start arriving the javascript engine starts processing the code (well, the web browser starts sending the data from to the assigned program or compiler). 一旦javascript文件开始到达,javascript引擎就开始处理代码(嗯,Web浏览器开始将数据从发送到分配的程序或编译器)。 Once that starts up, it kind of runs as a separate program, which have some hooks into the web browser. 一旦启动,它就可以作为一个单独的程序运行,它与Web浏览器有一些联系。 And while that is still booting up, the browsers rendering engine will be processing the html code, create DOM objects and all other bits it needs. 尽管仍在启动,但浏览器呈现引擎将处理html代码,创建DOM对象和它所需的所有其他位。

As for the server, in almost all cases, the data needs to be sent from the server side to the browser as a text string, the browser then forwards it to the program running in the compiler or interpreter. 对于服务器,几乎在所有情况下,都需要将数据作为文本字符串从服务器端发送到浏览器,然后浏览器将其转发到在编译器或解释器中运行的程序。 If you trace or snoop network traffic you will se the JSON strings go past. 如果您跟踪或监听网络流量,您会发现JSON字符串会过去。

You will also have to wait until your program or code is running before you send the JSON, as to the web browser, it is just a text string, test it by making one of the api calls in the browser directly. 在发送JSON之前,您还必须等到程序或代码运行后才能发送,对于网络浏览器而言,它只是一个文本字符串,请直接在浏览器中进行api调用之一对其进行测试。

This is why you will need to let the java code start first, then make a api call to the server, then read the returning string into some kind of structure. 这就是为什么您需要首先让Java代码启动,然后对服务器进行api调用,然后将返回的字符串读入某种结构的原因。

Hope this helps 希望这可以帮助

暂无
暂无

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

相关问题 如何通过JSON将表数据从HTML前端和HTML前端传递到Django中的服务器端? - How to pass a table data from and HTML front end to the server side in Django through JSON? 如何将反应前端的objectId传给服务端graphql? - How do I pass in objectId from the react front end to server-side graphql? 实现基于RESTful API的Web App。 我需要服务器端框架吗? - Implementing a RESTful API based Web App. Do I need a server-side framework? Koa server--如何在中间件之间传递服务器中的数据,以便我可以将它传递给前端 - Koa server-- How to pass data in the server between middlewares so I can pass it to the front end 如何将序列化器字段从 RESTful API - DRF (Django) 传递到前端框架? - How to pass Serizalizer Fields to front-end Framework from RESTful API - DRF (Django)? 基于路由参数的服务器端渲染动态页面 - Server Side Render Dynamic Page based on Route Param 在Node.js中使用AJAX(作为RESTful API)来获取JSON文件并根据前端中的哪个按钮进行解析 - Using AJAX (as RESTful API) in Node.js to GET a JSON file and parse it based on which button in the front end is clicked 服务服务器端配置到前端,滑轨/角度 - serving server side configurations to the front end, rails/angular 如何将标题和图像文件从前端 (React.js) 传递到后端 API (Spring Boot)? - How can I pass both the title and an image file from my front end (React.js) to my backend API (Spring Boot)? 如何在基于ReactJS的节点应用程序中从外部API获取服务器端的数据? - How to fetch data on server-side from external API within ReactJS-based node application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM