0

我正在使用 JQuery 加载功能来加载我的部分页面。我可以从加载它的页面中的该页面访问变量吗?例如

  • 页面A使用JQuery加载函数加载B

  • 页面 B 在名为 pageB_var 的上下文中加载并设置一个变量,该变量包含一个 django 对象

  • 然后页面 A 可以通过执行 {{pageB_var}} 访问此变量,因为它已添加到上下文中

如果不是,最好的方法是什么?

谢谢

4

3 回答 3

1

No. Page B's rendering context is irrelevant and unreachable by the time you get B's response.

Here's what happens: Page A is rendered in the server. during this time, its context exists. when the server is done rendering it, it sends the rendered page to the client. the client web browser then runs the javascript including your jquery load() to call the server again and tell it to render B. at this point the process that rendered page A doesn't exist anymore, so for page B to send stuff to page A's rendering you would have to make time travel....

The way to do this, is for page B to return a JSON object, and then use the (javascript) callback function given to load() to render changes to the page based on this JSON response from B.

于 2010-05-06T11:13:00.083 回答
0

一旦页面 A 被发送到浏览器,它大部分是固定在内存中的。您必须使用 JavaScript 中的 DOM 函数来修改它显示的内容。话虽如此,您可以从视图返回 JSON 以调用页面 B,然后对其进行解码并将其插入页面 A。

于 2010-05-06T11:10:36.563 回答
0

It sounds like you're using an asynchronous request to update part of a page, receiving a partial HTML response and replacing part of the markup of your page with this response.

You've found that this obviously limits you to updating information contained within that partial page.

Consider instead providing a JSON response to your asynchronous request, containing all the information you need, and then updating the necessary HTML in any part of the page by manipulating the DOM client-side, with JavaScript. Think of the JSON as being your context for this purpose.

于 2010-05-06T11:12:10.743 回答