简体   繁体   English

从ajax调用中获取父URL参数?

[英]Retrieving parent url parameters from ajax call?

say I have a view at 说我有一个观点

url(regex=r'^(?P<slug>[-\w]+)/$'...) 

and in the template of that view I have an ajax post to an api (via tastypie) at 在该视图的模板中,我有一个Ajax帖子发布到api(通过freshpie)处

url(regex=r'^post/$'...)

Is there a way in the tastypie api resource functions to pull out the url slug of the parent view? tastypie api资源功能中是否有办法拉出父视图的url插件?

Note: I prefer a django kind of way, not something like passing in document.URL 注意:我更喜欢django的方式,而不是像传递document.URL这样的方式

HTTP is stateless. HTTP是无状态的。 Meaning that each request is processed independently of other requests. 意味着每个请求都独立于其他请求进行处理。 Therefore when the ajax call is made, it is completely independent of the parent page. 因此,进行ajax调用时,它完全独立于父页面。 As a matter of fact, it does not even have to be an ajax call from the parent page. 实际上,它甚至不必是父页面的ajax调用。 You can just separately make the ajax call outside of the parent page. 您可以在父页面之外单独进行ajax调用。

The only solution is to manually pass something to the ajax call. 唯一的解决方案是手动将某些内容传递给ajax调用。 You can pass the parent page URL manually (using JS) to the ajax call. 您可以手动(使用JS)将父页面URL传递给ajax调用。 Alternatively when the parent page is being rendered, you can store the page URL in the session. 或者,当呈现父页面时,您可以将页面URL存储在会话中。 Then in an ajax call, you can retrieve that URL and use it as you like. 然后,在ajax调用中,您可以检索该URL并根据需要使用它。 I would however recommend the first method because the other one is more error-prone and possibly not as secure. 但是,我建议使用第一种方法,因为另一种方法更容易出错,并且可能不太安全。

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

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