简体   繁体   English

如何通过初始请求发送有效载荷数据?

[英]How do you send payload data with initial request?

Lets say, for example, we have a page that has a table with list of users. 比方说,例如,我们有一个页面,其中包含一个带有用户列表的表。 I could render the HTML with the table of users, but that isn't very friendly with Angular. 我可以使用用户表来呈现HTML,但这与Angular不太友好。 I can also AJAX back to the server for the data, but then I have to wait for the initial page to render and make an additional HTTP request, which adds additional delay. 我也可以将AJAX返回到服务器以获取数据,但是随后我必须等待初始页面呈现并发出其他HTTP请求,这增加了额外的延迟。

Is there a way to send my payload data along with my initial request so I don't have to AJAX for the data and I can still use angular binding (eg ng-repeat)? 有没有一种方法可以将我的有效载荷数据与初始请求一起发送,这样我就不必对数据进行AJAX,并且仍然可以使用角度绑定(例如ng-repeat)?

The only way to access server data without executing Javascript is to have the server include that data in the initial request for the web page. 在不执行Java脚本的情况下访问服务器数据的唯一方法是让服务器在初始网页请求中包含该数据。 This would require using a server side language such as PHP, Ruby, or ASP.NET that can query the data, add it to your web page, and send the updated web page in it's inital http resonse. 这将需要使用服务器端语言(例如PHP,Ruby或ASP.NET)来查询数据,将其添加到您的网页,并以其初始的HTTP响应发送更新的网页。

However, this concept completely undermines the purpose of using a client side framework such as AngularJS. 但是,此概念完全破坏了使用客户端框架(例如AngularJS)的目的。 Angular wants to be in charge of your data access, and I would recommend letting it be. Angular希望负责您的数据访问,我建议您随它去。 Angular doesn't know about Data that it didn't retrieve, so you are going to have to also dynamically modify your page scripts to tell Angular about the data that the server retrieved. Angular不知道它没有检索到的数据,因此您还必须动态地修改页面脚本,以告知Angular服务器检索到的数据。 This is all going down a dark road that you do not want to go down. 这一切都是沿着您不想走的黑暗道路走。 you will simply be doing double work. 您将只是做双重工作。

If your concern is that your users will be see a brief delay between the page load and the initial data loading, you even have the option to keep the screen blank until the initial data loads using things directives like ng-cloak and ng-bind. 如果您担心用户在页面加载和初始数据加载之间会出现短暂的延迟,您甚至可以选择使用ng-cloak和ng-bind之类的东西将屏幕保持空白,直到初始数据加载为止。 If you are concerned about the delays in retrieving data after the initial page load, remember that a full-page HTTP request and full page reload takes much longer than an ajax call that only updates a single part of your page. 如果您担心初始页面加载后检索数据的延迟,请记住,整页HTTP请求和整页重新加载所花费的时间比仅更新页面的一部分的ajax调用要长得多。

If you don't want to be using Ajax calls, you don't want to be using AngularJS. 如果您不想使用Ajax调用,则不想使用AngularJS。

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

相关问题 您如何将带有数据的请求发送到 ReactJS 中的 API 然后将响应? - How do you send a request with data to an API in ReactJS that will then respond? 如何链接GCM chrome推送通知和有效载荷数据? - How do you link GCM chrome push notifications and payload data? 如何在初始请求中提供JSON有效负载,以在JS应用中使用 - How to serve JSON payload in the initial request, for use in JS app Angular $ resource服务以表单数据而不是请求有效载荷发送数据 - Angular $resource service send data in Form Data instead of Request Payload 如何发送数组作为(jQuery)Ajax请求的一部分 - How do you send an array as part of an (jquery) ajax request 你如何从本地主机发送跨源请求? - How do you send a cross origin request from localhost? 如何通过jQuery POST请求发送原始字符串? - How do you send a raw strings through a jQuery POST request? 发出 Fetch API 请求时无法将对象作为有效负载数据发送 - Unable to send objects as payload data while making an Fetch API request 如何在 AJAX 请求后使用反应上下文 api 值更新 Formik 初始值? - How do you update Formik initial values with the react context api values after an AJAX request? 如何从请求有效载荷中将数据发布为json? - How to post data as json from request payload?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM